Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

What is Web Driver:-

It is the most important component of Selenium, which is used to create script using Java, C#, PYTHON, etc to test web applications.

Web drivers provide better customization and advance testing operations on Web applications.

It also uses locator, command to create an automation test script.

Web driver script can be executed in all standard Web browsers using Web browser Client based software.



 How to create infrastructure for Web Driver under selenium:-

1)  Install Java and Eclipse Software


2)  Download Selenium to manage Web Driver



3)  Download Browser Client to run the script for all particular browsers.








WebDriver Command:-

The command is used to perform an operation on a web element, for example, if we click on button then click is the command which will work under Button Web Element.


Command-List Of Web Driver:-

1) GET COMMANDS in Web Driver:-

 1.1) get():-   It is open web page using web URL

    WebDrier driver = new ChromeDriver();
    driver.get("url")


1.2)  getTitle():-  It is used to get the title of current web pages

       WebDrier driver = new ChromeDriver();
       driver.getTitle("url")

1.3)  getCurrentUrl():- 
It is used to display current web Url of an application

       WebDriver driver = new ChromeDriver();
       String s = driver.getCurrentUrl()


import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.*;
public class CountHyperlink {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("http://newtours.demoaut.com/");

  driver.findElement(By.linkText("CONTACT")).click();
  System.out.print(driver.getCurrentUrl());
  driver.close();

}

}
1.4) getAttribute():-   It is used to fetch attribute of current web element

      <input type="text"  width="100"  id="txt"   />

      WebDriver driver = new ChromeDriver();
      WebElement ele = driver.findElement(By.name("xyz"));
      String s = ele.getAttribute("width");
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.*;
public class CountHyperlink {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("http://newtours.demoaut.com/");

   WebElement ele= driver.findElement(By.linkText("CONTACT"));
   System.out.print(ele.getAttribute("href"));
  driver.close();

}

}

1.5) getPageSource():-  It is used to get the Source code of an application.

        WebDriver driver = new ChromeDriver();
       String s = driver.getPageSource();
 import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.*;
public class CountHyperlink {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
  WebDriver driver = new ChromeDriver();
  driver.get("http://newtours.demoaut.com/");

  // WebElement ele= driver.findElement(By.linkText("CONTACT"));
   System.out.print(driver.getPageSource());
  driver.close();

}

}

1.6) getText():-  It is used to get the internal text of HTML element for example if we want to fetch inner text of division then it will be managed by getText();

Example of  Get-Command of Web Driver:-

package scs;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverExample2 {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://newtours.demoaut.com/");
        String s = driver.getPageSource();
        System.out.print(s);
        String s1 = driver.getCurrentUrl();
        System.out.print(s1);
        WebElement s2 = driver.findElement(By.name("userName"));
        String sz=s2.getAttribute("size");
        System.out.print("size is "+sz);
       

    }

}




2) Navigate Command's in Web Driver:-

It is used to navigate from one web page to another then we can use navigate commands


1 driver.navigate.to():-    it is used to redirect into a particular web page using a web URL.



2  driver.navigate.back():-  It is used to press the back button and revert to the main page.


3 driver.navigate.refresh():-  It is used to refresh the content in web pages


4 driver.navigate.forward():-  It is used to press the forward button of the browser and display forward web pages of an application.






 package scs;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NavigationCommand {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://shivaconceptsolution.blogspot.com/");
        driver.navigate().to("https://shivaconceptsolution.blogspot.com/search/label/JAVASCRIPT%20LECTURE%20BY%20SHIVA%20SIR");
        Thread.sleep(2000);
        driver.navigate().back();
      

    }

}




3) switch to Command:-

It is the switch from one window to another in the same web page


The switch will load a particular container hence we should provide container name in switch command.


Q)Create Automation Script to Switch Second Frame and click on a particular Link?

http://demo.guru99.com/selenium/deprecated.html


Complete Code of Selenium Web Driver:-

package scs;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SwitchToExample {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/selenium/deprecated.html");
driver.switchTo().frame("classFrame");
WebElement ele = driver.findElement(By.linkText("Tree"));
ele.click();


}

}



SwitchTo Command also used to handle the popup box of alert().


Q) Create Automaton Script to get an internal test of the alert box?

package scs;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class AlertBoxExample {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://output.jsbin.com/usidix/1");
WebElement ele = driver.findElement(By.cssSelector("input[value=\"Go!\"]"));
ele.click();
String s = driver.switchTo().alert().getText();
System.out.print(s);
driver.switchTo().alert().accept();
driver.close();


}

}


         


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post