First selenium sample code using eclipse

Let's see how we can guide selenium to work on a web application,

As we come to know from our old posts, selenium identifies elements (WebElement) on basis of the html tags and their attributes.

Let's take the mercury tour application as our AUT(application under test) and firefox browser,
For other browsers like IE and Chrome, please refer working with multiple browsers.

TestCase: register and check if the user name and password is correct to log in the site.
Steps:
  • Go to the url
  • Click on the register link
  • Enter required data to register site
  • Go to home page
  • Enter the username and password and click on the submit button.
  • Verify if logged in
For more details of eclipse setup with selenium, please refer eclipse setup for selenium.

For each code line below, have comments to understand the need for that line.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package firsttestcase.mercurytour;

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.openqa.selenium.support.ui.Select;

public class mercurytour{
 public static WebDriver driver;

 public static void main(String[] args) {
  //go to the url:
  //Launch firefox browser 
  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 sec, throw exception
  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");
  //Enter mailing information as above 
  //Select the country, create a select object and then work on that object
  Select s = new Select(driver.findElement(By.name("country")));
  s.selectByVisibleText("INDIA");  
  //Enter user name and password
  driver.findElement(By.name("email")).sendKeys("12345");
  driver.findElement(By.name("password")).sendKeys("12345");
  driver.findElement(By.name("confirmPassword")).sendKeys("12345");
  //click on submit
  driver.findElement(By.xpath("//input[@name='register']")).click();
  
  driver.findElement(By.linkText("Home")).click(); 
  
  //Enter the username and password and click on the submit button.
  driver.findElement(By.name("userName")).sendKeys("12345");
  driver.findElement(By.name("password")).sendKeys("12345");
  driver.findElement(By.xpath("//input[@name='login']")).click();
  
  //verify if login successful
  WebElement form_findflight = driver.findElement(By.xpath("//form[@name='findflight']")); 
  if (form_findflight.isDisplayed())
  {
   System.out.println("Login successful!");
  }  
 }
}

1 comment: