In this post, we will see how to create a maven project in eclipse and run selenium java tests using testNG.
Why to use Maven
Maven helps to easily update libraries (jar files) required to run selenium tests and also easy to maintain the jar files.
Note : If you want to manually add the jar files to your project, then follow here
Prerequisites
Eclipse – https://www.eclipse.org/downloads/packages/
Chose the eclipse for java developers version which comes with inbuilt maven and testNG, if not then you can install maven and testng from eclipse marketplace (Help > marketplace)
pom.xml
Get the pom.xml from here [Updated with latest version]
You can find the all version of libraries from here
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="SampleSuite"> <test name="SampleTest"> <classes> <class name="com.practice.SeleniumTest.SampleSelTest" /> </classes> </test> </suite>
Maven project
Create a maven project in eclipse, follow here for more details
SampleSelTest.java
package com.practice.SeleniumTest; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class SampleSelTest { WebDriver driver; @Test public void TestMethod() { //Launch firefox browser System.setProperty("webdriver.gecko.driver", "/Users/sunilkumarpatro/sel/geckodriver"); driver = new FirefoxDriver(); //maximize the browser driver.manage().window().maximize(); //Implicit wait, wait for at least some time (10 sec) to identify an element, //if can't find the element with in 10 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //open the url or AUT driver.get("http://newtours.demoaut.com/"); //Click on the register link: WebElement lnk_register = driver.findElement(By.linkText("REGISTER")); lnk_register.click(); //Enter details on the provided text boxes by using sendkeys method. WebElement txtBox_firstname = driver.findElement(By.name("firstName")); txtBox_firstname.sendKeys("John"); //or directly we can identify element and act on that in one line driver.findElement(By.name("lastName")).sendKeys("Clark"); driver.findElement(By.name("phone")).sendKeys("2342342341"); driver.findElement(By.name("userName")).sendKeys("sunilpatro1985@gmail.com"); } }
Run the testng.xml file to execute selenium java script.
2 Comments