Chapter 1_4:First Webdriver Script

Script Creation

For script creation, we would be using “Testology” Package created in the eclipse and “mercurytours.com” as the application under test (AUT).

below are the steps to create first webdriver program

  1. Launch the browser and open “mercurytours.com”.
  2. Enter the username and password.
  3. click on sign in button.
  4. close the browser.

Below program will help you to understand all above steps.

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class logindemo 
   {
	public static void main(String[] args) {
		System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("http://newtours.demoaut.com/");
		driver.findElement(By.linkText("SIGN-ON")).click();
		WebElement name = driver.findElement(By.name("userName"));
		name.sendKeys("Testology");
		// get the webelement corresponding to password (passwordfield)
		WebElement password = driver.findElement(By.name("password"));
		password.sendKeys("Testology12");
		// to click on submit button
WebElement submitbutton=driver.findElement(By.cssSelector
("input[name='login'][value='Login']"));
		submitbutton.click();
		// close chrome
		driver.close();
	}
}