In the following post I will go step by step and explain how to write your first test case using Selenium Webdriver and execute it using TestNG.
1. Right click on the src folder ->New -> Package.
2. Provide package name something like com.stm.test and click “Finish”.
3. Right click on the newly created package – > New -> Class.
4. Provide class name as “RegistrationTest” and click Finish.
5. Write the code given below for your first test.
I am taking an example of automating register user functionality for demo site http://newtours.demoaut.com
The code for the first test is as follows:
package com.stm.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class RegistrationTest
{
@Test //This is TestNG annotation
public void testRegister()
{
WebDriver driver = new FirefoxDriver();
driver.get(“http://newtours.demoaut.com/”);
driver.findElement(By.linkText(“REGISTER”)).click();
driver.findElement(By.name(“firstName”)).sendKeys(“User1”);
driver.findElement(By.name(“lastName”)).sendKeys(“Surname1”);
driver.findElement(By.name(“phone”)).sendKeys(“123456789”);
driver.findElement(By.name(“userName”)).sendKeys(“[email protected]”);
driver.findElement(By.name(“address1”)).sendKeys(“Test Address”);
driver.findElement(By.name(“city”)).sendKeys(“Test City”);
Select select = new Select(driver.findElement(By.name(“country”)));
select.selectByVisibleText(“ANGOLA”);
driver.findElement(By.name(“email”)).sendKeys(“[email protected]”);
driver.findElement(By.name(“password”)).sendKeys(“user1”);
driver.findElement(By.name(“confirmPassword”)).sendKeys(“user1”);
driver.findElement(By.name(“register”)).click();
driver.close();
driver.quit();
}
}
6. After finishing the test right click on the test and click on RunAs – >TestNG Test
7. After executing the test select the project and press F5 to refresh the project. A new folder “test-results” will get created which will show you the results for the execution. Right click on index.html->open with->web browser to see the execution report.
If you like this post hit like button for facebook and share it!!