Why is Selenium Webdriver with Java a great choice for web service integration testing?
Selenium Webdriver is an amazing tool allowing you to interact with browsers installed on your system. It allows you to create test scenarios like “open this page – check if an element is visible – click on this button – check what happened by examining the DOM”. This testing approach is known as behavior-driven development (BDD). In other words – you write a separate test program which replaces the manual tests you would have performed during the development process.
Selenium is a tool that controls a web browser. To write test scenarios you can use a programming language like JavaScript or Java. A full list of supported languages can be found on this page.
Why Java? Java is one of the most popular programming languages. The community has developed many libraries for interacting with databases, web servers, file servers, Cloud Services and other resources you may need to access from your test application. This plethora of libraries will allow you to create powerful test scenarios .
Creating a Java Selenium project with Maven
The easiest way to write your first Selenium test scenario is to create a Java Maven project and include the Selenium WebDriver as a dependency. Fortunately Maven supports archetypes, predefined project templates which contain a minimal starting project structure. We will use one such archetype to get started quickly. We recommend using the IntelliJ IDEA.
To create your first Selenium Java project follow these steps:
- Start a new Project, select Maven as the project type
- Check the option for “Create from archetype”.
- Click on the “Add Archetype…” button.
- In the “Add Archetype” dialog window enter the following values:
- GroupId: ru.stqa.selenium
- ArtifactId: webdriver-testng-archetype
- Version: 4.0
- Leave Repository blank
- Select the newly added archetype in the list and click “Next”
- Enter your project’s Maven attributes:
- GroupId: com.my-first-level-domain.my-second-level-domain // This can be a series of identifiers particular to your company, project and other naming conventions your team has established, for example ‘com.yojimbocorp.seleniumdemo’
- ArtifactId: selenium-tests
- Leave Version as the default value
- GroupId: com.my-first-level-domain.my-second-level-domain // This can be a series of identifiers particular to your company, project and other naming conventions your team has established, for example ‘com.yojimbocorp.seleniumdemo’
- Click “Next” until it changes to “Finish” (you may want to change the project name/location from the defaults).
- Your new project will open and maven will begin executing, once its complete, it will prompt you to “Import Changes” in the bottom right of your window.
- Click on “Import Changes” and wait while dependencies are imported/resolved.
Modify TestNgTestBase to handle missing Selenium Grid URL
Your project already contains demo test classes, so you can try running tests out of the box, but before your first test modify the TestNgTestBase.initWebDriver method as follows
Replace
driver = WebDriverPool.DEFAULT.getDriver(gridHubUrl, capabilities);
with
try { if (null == gridHubUrl || "".equals(gridHubUrl)) { driver = WebDriverPool.DEFAULT.getDriver(capabilities); System.out.println("If you need Selenium Grid, please start Selenium " + "Server standalone and check file debug.properties and URL of grid.url."); } else { driver = WebDriverPool.DEFAULT.getDriver(new URL(gridHubUrl), capabilities); } } catch(MalformedURLException ex) { System.out.println("Invalid url specified in grid.url:" + ex.getMessage()); }
Customizing testing properties
Testing configuration is defined in the pom.xml file. Open the file and find the following section:
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile> </suiteXmlFiles> <systemPropertyVariables> <application.properties>/application.properties</application.properties> </systemPropertyVariables> </configuration> </plugin>
As you can see the file name used for defining testing properties, in this example its application.properties. If you open this file you will see parameters with placeholder values, e.g.
capabilities=${capabilities} site.url=${site.url} grid.url=${grid.url}
Back in pom.xml you will find a section of profiles for different browsers, each with a capabilities child tag. Firefox is set as the default browser.
<profile> <id>firefox</id> <properties> <capabilities>/firefox.capabilities</capabilities> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile>
If you want to use one of the other profiles you can specify the profile in the Maven test command, e.g.
mvn -P chrome,localhost,nogrid test
To do it in IntelliJ IDEA open the Maven project panel by clicking on “Maven Projects” button:
If you expand “Run Configuration”, you will see the initial configuration created (if you don’t see one go to the top menu, select Run->Edit Configurations click on the + and add a Maven configuration using a similar dialog to the one shown below). In the popup window you can specify the browser profile you prefer:
Running tests
Now you are ready to run your first test. Expand “Run Configuration” in the Maven project tree and right click on “selenium tests”. In context menu press on “Run”.
You will see messages displayed in the Run panel (bottom of the IDE by default). You will also observe that Chrome is briefly opened and closed during the test run.
Congratulations! You are now ready to write some tests.
Contributed by Vlad (view original post here).
Leave a Reply