Blogs Hub

by AskGif | Jun 26, 2019 | Category :coding

Start your web tests with Selenium2, Maven, Testng

Start your web tests with Selenium2, Maven, Testng

<p>Every time you are playing around with web tests you think: why have I waited so long. Doesn&rsquo;t matter, you have done it and that&rsquo;s it what matters.</p> <p>&nbsp;</p> <p>The last time I used Selenium 1 which is very often used for web tests. Selenium 1 is great but has a few limitations. It is based around an injected Javascript library &ndash; the Selenium core. It works, but starting the browser with the Selenium core is often a little bit tricky. With every browser update, you hope that it is still running or hope for the update, which needs to be included in the Maven plugin, is pushed to the Maven mirrors, is really working on all systems, &hellip;</p> <p>&nbsp;</p> <p>But now there is Selenium 2 with the merge of Webdriver. It is no longer a Javascript core, it is a native browser plugin. And now you are much deeper inside the browser. It feels more stable from the first second and it works really well. With the Webdriver part of Selenium 2, there is a new API. But it isn&rsquo;t very complicated, just different.</p> <p>&nbsp;</p> <p>I can give you a damn short example which works really well for me and runs through our entire webpage. The great part: it takes screenshots of the complete page (not just the visible part) and stores them in the target folder.</p> <p>&nbsp;</p> <p>First of all, the Maven pom:</p> <pre class="language-markup"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;de.viaboxx.tests&lt;/groupId&gt; &lt;artifactId&gt;integrationtest&lt;/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.testng&lt;/groupId&gt; &lt;artifactId&gt;testng&lt;/artifactId&gt; &lt;version&gt;5.13.1&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt; &lt;artifactId&gt;selenium&lt;/artifactId&gt; &lt;version&gt;2.0a6&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt; &lt;artifactId&gt;selenium-firefox-driver&lt;/artifactId&gt; &lt;version&gt;2.0a6&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;skip&gt;true&lt;/skip&gt; &lt;/configuration&gt; &lt;executions&gt; &lt;execution&gt; &lt;phase&gt;integration-test&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;test&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;skip&gt;false&lt;/skip&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;source&gt;1.5&lt;/source&gt; &lt;target&gt;1.5&lt;/target&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt;</code></pre> <p>And it follows the whole logic for the test placed under src/test/java/de/viaboxx/tests/SeleniumTest.java:</p> <pre class="language-java"><code>package de.viaboxx.tests; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; import java.io.File; import java.io.IOException; public class SeleniumTest { private static final String FOLDER_NAME = "target/screenshots/"; @Test public void website() throws IOException, InterruptedException { FirefoxDriver driver = new FirefoxDriver(); driver.get("http://www.viaboxxsystems.de"); Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Innovation"); FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "startpage.png")); driver.findElementByLinkText("Team").click(); Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Team"); FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "team.png")); driver.findElementByLinkText("Blog").click(); FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "blog.png")); driver.findElementByLinkText("Impressum").click(); FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "impressum.png")); driver.close(); } }</code></pre> <p><span style="color: #7e7e7e; font-family: 'PT Sans', sans-serif; font-size: 15px;">Run it with:</span></p> <pre class="language-markup"><code>mvn integration-test</code></pre> <p>Readable isn&rsquo;t it. And that&rsquo;s all &ndash; a complete working infrastructure for your web tests. The example works with Firefox, but you can also run headless with HtmlUnit, or make your tests running in IE or Chrome. The great thing: just put the application in your continuous integration server and run it on your defined build agent.</p> <p>&nbsp;</p> <p>After just one evening I fall in love with the new Selenium 2. Why? Because it works much more stable in kind of browser starting and running. I think it is a great way to go.</p>

read more...