Thursday 1 March 2018

How To Use Selenium WebDriver Methods In Real time

                                                               WebDriver Methods:
========================================================================

Selenium webdriver methods are used to perform operations on the browser and web elements.
Using Web element locators and Webdriver methods the test scripts can be written.

The following are the webdriver methods:
  •  get()
  •  navigate()
  •  findElement()
  •  findElements()
  •  getCurrentURL()
  •  getTitle()
  •  getWindowHandle()
  •  getWindowHandles()
  •  close()
  •  quit()


get():

  •  This method is used to launch a URL in the current driver instance.
  •  This method will launch the URL and waits until all the elements of a webpage are loaded.

Syntax:

driver.get(Fully Qualified URL)

Example:

driver.get(“https://www.google.co.in”)

Note:

Fully qualified URL will start with https or http.

navigate():


  •  This method is also used to launch a url in the driver instance and waits for all the elements to be loaded.
 But this method also supports, the features such as

  •  Backward
  •  forward
  •  refresh

Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.dezlearn.com/testingpage/");
driver.findElement(By.xpath(".//*[@id='popup']/span")).click();
driver.findElement(By.xpath(".//*[@id='menu-item-258']/a").click();
driver.navigate().refresh();
driver.navigate().back();

Note:

  • back(), forward() methods will navigate to the pages based on the history of thepage navigations in the driver instance.

FindElement():

  • This method is used to find a web element in the current document of driver instance based on the node properties  
 The following are the sub methods that findElment supports


  •  sendKeys – to enter text
  •  click – to perform click operation
  •  clear – to clear the existing contents
  •  isDisplayed – to verify the existence – true/false
  •  isEnabled – to verify if the field is enabled – true/false
  •  isSelected – if the checkbox is selected – true/false
  •  getAttribute – to get the attribute value/property value.


Example1:

driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("Rafi@14467");

Example2:

driver.findElement(By.xpath("//input[@name='Login']")).click();

Example3:

driver.findElement(By.xpath("//input[@name='userName']")).clear();

driver.findElement(By.xpath("//input[@name='userName']")).sendKeys("abcd123");

Example4:

boolean flagdisplayed;
flagdisplayed = driver.findElement(By.xpath("//input[@name='userName']")).isDisplayed();
System.out.println(flagdisplayed);

Example5:

boolean flagEnabled;
flagEnabled = driver.findElement(By.xpath("//input[@name='userName']")).isEnabled();
System.out.println(flagEnabled);

Example6:

boolean flagSelected;
flagSelected = driver.findElement(By.xpath("//input[@name='checkbox']")).isSelected();
System.out.println(flagSelected);

Example7:

String value = "";
value = driver.findElement(By.xpath("//input[@name='login']")).getAttribute("width");
System.out.println(value);

Note:


  • If the element description is matching more than one node, by default this method will points to the first occurrence.
  •  In order to point out to specific occurrence, we need to use index in xpath.

findElements():

 This method is used to perform action on a group of web elements.
 This method will return the web elements in the form of a list.
 From the list, we have to iterate using for each loop and then perform action on each webelement.

Syntax:

   List<WebElement> objectname = driver.findElements(by.Xpath("xpath Expression"));


  • To get the number of matching web elements, we can use objectname.size();


1.Program to get the names of all the radio buttons present on a webpage:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
List<WebElement> allitems = driver.findElements(By.xpath("//input[@type='radio']"));
for(WebElement ele:allitems)
{
System.out.println(ele.getAttribute("name"));
}

2.Program to get the number of links displayed in a webpage:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com/");
List<WebElement> allitems = driver.findElements(By.xpath("//a"));
System.out.println(allitems.size());

3.Program to get the names of all the radiobuttons:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://newtours.demoaut.com/");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
List<WebElement> allitems = driver.findElements(By.xpath("//input[@type='radio']"));
for(WebElement ele:allitems)
{
System.out.println(ele.getAttribute("name"));
}

getCurrentURL():

This method is used to get the current URL of the webdriver instance.

Example:

System.out.println(driver.getCurrentUrl());

getTitle():

This method is used to get the title of the webdriver instance.

Example:

WebDriver driver=new FirefoxDriver();
driver.get("http://www.gmail.com");
String Title=driver.getTitle();
System.out.println("Dash Board  Page Title "+Title);

getwindowhandle():

This method is used to get the runtime generated page id of the driver instance.

Example1:

System.out.println(driver.getWindowHandle());

Example2:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://newtours.demoaut.com/");
System.out.println(driver.getCurrentUrl());
System.out.println(driver.getTitle());
System.out.println(driver.getWindowHandle());

Output:

http://newtours.demoaut.com/
Welcome: Mercury Tours
{38ed9a6c-dedc-4d73-a86d-1242d22f0470}

getwindowhandles():

This method is used to get all the window handles of the diver instance.
The window handles will be returned in the form a String set.

Code to get the window handles:

Set<String> allhandles = driver.getWindowHandles();
for(String str1:allhandles)
{
System.out.println(str1);
}

close():


  • Close method is used to close only the current window of the driver instance.

 Example:

driver.close(); 

quit():


  • quit method will close all the windows of the driver instance.

 Example:

driver.quit(); 



For Any Software Testing Related Courses Please Contact Our Number                           8790002007            

1 comment:

  1. The Star Casino is Your Top Secret | DrMCD
    The Star Casino has been a top-class casino for 성남 출장샵 some 충주 출장마사지 time now. With 속초 출장마사지 a 제주도 출장샵 strong focus on the entertainment needs of the Star in Las 경기도 출장샵 Vegas, we can be sure

    ReplyDelete