Selenium server installation
We will start by Selenium server which is connected to browser. In my example is used Internet Explorer.
Download Selenium server from Selenium. It will be only simple jar like selenium-server-standalone-2.35.0.jar. Next we need WebDriver driver for IE. It is under "The Internet Explorer Driver Server" on the same page. You will download IEDriverServer_Win32_2.35.1.zip file, which contains IEDriverServer.exe.
Copy both files into one directory and create starting script, for example start.bat:
SET JAVA_HOME="c:\progra~2\Java\jdk1.7.0_17" SET PATH=%JAVA_HOME%\bin;%PATH% java -jar selenium-server-standalone-2.35.0.jar -Dwebdriver.ie.driver=IEDriverServer.exe -port 14444
Start server by created script. Result can be something like:
You can see line with Selenium server URL in console:
INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:14444/wd/hub
We will use it later. Server side is ready.
Create Maven project
Now we need some test. Create directory structure. Maven can help you:mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
-DarchetypeGroupId=org.apache.maven.archetypes
-DgroupId=com.mil -DartifactId=selenium-test -Dversion=1.0.0
Create class GooglePage.java under src/test/java/com.mil.selenium.page. This class represents "Page Object" pattern so it models page (Google.com) to object.
package com.mil.selenium.page; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; public class GooglePage { @FindBy(name = "q") private WebElement search; public void search(String query) { search.sendKeys(query); search.sendKeys(Keys.RETURN); } }Next class is TestNG test which will call Page Object class.
package com.mil.selenium.test; import java.util.List; import java.util.concurrent.TimeUnit; import org.jboss.arquillian.drone.api.annotation.Drone; import org.jboss.arquillian.graphene.spi.annotations.Page; import org.jboss.arquillian.testng.Arquillian; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.Test; import com.mil.selenium.page.GooglePage; public class GoogleTest extends Arquillian { @Page GooglePage page; @Drone private WebDriver driver; @Test public void testSearch() { try { driver.manage().timeouts() .setScriptTimeout(Integer.valueOf(10), TimeUnit.SECONDS); driver.manage().timeouts() .implicitlyWait(Integer.valueOf(10), TimeUnit.SECONDS); driver.manage().timeouts() .pageLoadTimeout(Integer.valueOf(10), TimeUnit.SECONDS); driver.get("http://google.com"); page.search("test query"); ListresultOfSearch = driver.findElements(By .cssSelector("li[class='g']")); Assert.assertTrue(resultOfSearch.size() > 0); } catch (Exception e) { } } @AfterClass public void afterClass() { driver.quit(); } }
Check testSearch() method. We open Google page, enter some query and check if there is some result. Simple.
If you want to close Internet Explorer browser after test, you need to call driver.quit(). See afterClass() method.
This test class cannot be started without Arquillian configuration. The following file is located in:
src\test\resources\arquillian.xml
<?xml version="1.0" encoding="UTF-8"?> <arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> <!-- Uncomment to have test archives exported to the file system for inspection --> <!-- <engine> --> <!-- <property name="deploymentExportPath">target/</property> --> <!-- </engine> --> <!-- Force the use of the Servlet 3.0 protocol with all containers, as it is the most mature --> <defaultProtocol type="Servlet 3.0" /> <!-- Example configuration for a remote JBoss AS 7 instance --> <container qualifier="jboss" default="true"> <!-- If you want to use the JBOSS_HOME environment variable, just delete the jbossHome property --> <!-- configuration> <property name="jbossHome">PATH_TO_JBOSS</property> </configuration--> </container> <extension qualifier="webdriver"> <property name="browserCapabilities">internetExplorer</property> <property name="javascriptEnabled">true</property> <property name="remoteReusable">true</property> <property name="remoteAddress">http://localhost:14444/wd/hub</property> </extension> <extension qualifier="graphene"> <property name="waitAjax">5</property> <property name="waitGui">5</property> <property name="waitModel">10</property> </extension> <!-- this is because of the dependency on testng-listener containing graphene and drone, which automatically starts selenium server --> <extension qualifier="selenium-server"> <property name="skip">true</property> </extension> </arquillian>
You do not need to change anything in this file. Only remoteAddress if you have Selenium server on different computer (mentioned previously).
Test run
Now you can test it. Try it by:mvn clean test
Your test will conect to configured Selenium server which will open Internet Explorer browser for your test.
Any issues? Ask please.
Maven project to download: selenium-test.zip
I've just looking for running Arquillian tests against a running site. This code just nicely does the work. Thanks!
ReplyDeleteNote: there is a issue with version of test-ng specified in pom.xml. Prefer use 5.14.6 version as helpfully mentioned there: https://community.jboss.org/message/596290