Scala is a JVM-based, statically- checked, multi-paradigm programming language — mixing up object-oriented programing with functional programing, which delivers a concise and elegant code style. Scala codes can be seamlessly compatible with Java projects as the complier of which turns all those .scala files into several .class binary files. And currently IDE plugins for Scala have been mature in some extent to support a smooth coding experience.
I’ve tried to integrate Scala code into an existing selenium-testng frame. It turned out easy to reach:
1. Environment:
JDK: 1.8
Eclipse: Luna
Scala: 2.11.5
Selenium: 2.42
Testng: 6.3.1
2. Add plugins to eclipse:
Eclipse -> help -> Eclipse Marketplace -> search ‘scala’ -> choose ‘scala IDE’ -> install -> restart
If the connection speed is restricted, you can also choose to download a complete copy from the official site: http://scala-ide.org/
3. Convert your project to an Scala project by:
Left click on your project -> Configure -> Add Scala Nature
After that you should see Scala libs are added in your project root:
4. It’s almost done! If you manage your project by Apache Ant, you need to download complete Scala suit from http://www.scala-lang.org/download/, add scala-compiler.jar to your project folder, add fsc task to compiler .scala files before main task is up in build.xml. And don’t forget to add the package/class in your testng.xml as well. You can find some path to follow.
Now, let’s see what we can do with our new friend:
1. Interfaces with project util in a Scala object:
e.g.
2. Define page object / elements with Scala lazy evaluation:
e.g. An instance of baidu homepage
Note that Scala objects can be read in Java code as well:
Thus it’s possible to have Java programmer and Scala programmer work together.
3. Extend WebElement class with your own actions by Scala implicit-type-conversion:
e.g. Always wait for elements loading complete before further actions:
4. Finally, you get a style of test scripts like what is shown below: