What is WebDriver, WebDriver Command

0

What is Web Driver:-
It is the most important component of Selenium, which is used to create scripts using Java, C#, PYTHON, etc to test web applications.
Web drivers provide better customization and advanced testing operations on Web applications.
It also uses a 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.
4) Open eclipse and create Java Project
5) Add Selenium Webdriver Jar file under project
  Right click on project ----> Properties ----> Java Build Path ---- > Click on library ---> Add External JAR ---->  Then press ok
6)  Extract chrome driver and put it under c:, d:
WebDriver Command:-
The command is used to perform an operation on a web element, for example, if we click on a 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 the current web Url of an application
       WebDriver driver = new ChromeDriver();
       String s = driver.getCurrentUrl()
Create Automation Script to print the URL of contact us and FAQ page?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetCurrentUrlExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://eroomrent.in");
driver.findElement(By.partialLinkText("Contact")).click();
String contacturl = driver.getCurrentUrl();
System.out.println("Contact page url is "+contacturl);
driver.findElement(By.partialLinkText("FAQ")).click();
String faqurl = driver.getCurrentUrl();
System.out.println("FAQ page url is "+faqurl);
driver.close();

}
}
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();
}
}
Example:-
Web driver script to check title?
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Helloworld {
public static void main(String args[])
{
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://eroomrent.in");
String title = driver.getTitle();
  if(title!="")
{
System.out.print("Title is set");
System.out.print(title);
}
else
{
System.out.print("Title is not set");
}
System.out.println(driver.getCurrentUrl());
}
}
Web driver example to check the title is valid or not?
package scs;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverExampleFirst {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://eroomrent.in");
String title = driver.getTitle();
if(title!="")
{
System.out.println("Title is set "+title);
if(title.length()<=60)
{
System.out.println("Title is valid");
}
else
{
System.out.println("Title is not valid");
}
}
else
{
System.out.println("Title is not set "+title);
}
}
}
1.4) getAttribute():-   It is used to fetch attributes of the 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.By;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.WebElement;
     import org.openqa.selenium.chrome.ChromeDriver;
public class AttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://eroomrent.in");
WebElement element = driver.findElement(By.className("img-fluid"));
String s = element.getAttribute("src");
System.out.print(s);
}
}
WebDriver Example to display image name using getAttribute():-
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 AttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://eroomrent.in/");
WebElement ele = driver.findElement(By.className("img-fluid"));
String s = ele.getAttribute("src");
int position = s.lastIndexOf("/");
String imgname = s.substring(position+1);
System.out.print(imgname);
}
}
Create Automation Script to find out image path and Alternate Text:-
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 GetAttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://eroomrent.in");
WebElement ele = driver.findElement(By.className("img-fluid"));
String src = ele.getAttribute("src");
String alt = ele.getAttribute("alt");
System.out.println("Path of image is "+src);
System.out.println("Alternate text of image is "+alt);
driver.close();
}
}
Attribute Example using user input?
package scs;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Scanner;
public class AttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
Scanner sc = new Scanner(System.in);
System.out.println("Enter url without http and https");
String url = sc.next();
driver.get("https://"+url);
List<WebElement> ele = driver.findElements(By.tagName("img"));
for(WebElement e:ele)
{
String s = e.getAttribute("src");
int position = s.lastIndexOf("/");
String imgname = s.substring(position+1);
System.out.println("path is "+s + "image name "+imgname);
}
}
}
Another Example of Image Count and Print Image Source:-
package scs;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CountTotalImage {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://eroomrent.in");
List<WebElement> ele = driver.findElements(By.tagName("img"));
System.out.println("Total Images are "+ele.size());
for(WebElement e:ele)
{
String s = e.getAttribute("src");
System.out.println(s);

}
}
}
1.5) getPageSource():-  It is used to get the Source code of an application.
       WebDriver driver = new ChromeDriver();
       String s = driver.getPageSource();
Example:-
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 AttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://eroomrent.in");
System.out.print(driver.getPageSource());
}
}
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 AttributeExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://eroomrent.in");
WebElement element = driver.findElement(By.tagName("h1"));
String s = element.getText();
System.out.print(s);
}
}
1.7) getCssValue():-
   It is used to get the attribute CSS property, for example, we want to display the color of textfield.
<input type="text"  style='color:red'  />
Q) create an automation script to get the color, width, and background color of the description text field?
Answer:-
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetCssValueExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shivaconceptsolution.com/test.html");
WebElement element = driver.findElement(By.cssSelector("input[value=click]"));
String data = element.getCssValue("color")+ element.getCssValue("background-color")+ element.getCssValue("width");
System.out.print(data);
driver.close();
}
}
1.8) getLocation():-   It will return x and y co-ordinate of particular web element?
public class GetCssValueExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shivaconceptsolution.com/test.html");
WebElement element = driver.findElement(By.cssSelector("input[value=click]"));
//String data = element.getCssValue("color")+ element.getCssValue("background-color")+ element.getCssValue("width");
String data = element.getLocation().toString();
System.out.print(data);
driver.close();

}
}
1) Another Example of getLocation() to check the alignment of RadioButton?
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 CheckRadioButtonAligment {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver(); 
driver.get("https://shivaconceptsolution.com/test.html");
WebElement ele = driver.findElement(By.cssSelector("input[value=FeMale]"));
String rblocation2 = ele.getLocation().toString();
System.out.println(rblocation2);
WebElement ele1 = driver.findElement(By.cssSelector("input[value=Male]"));
String rblocation1 = ele1.getLocation().toString();
System.out.println(rblocation1);
int x1 = ele1.getLocation().x;
int x2 = ele.getLocation().x;
if(x1==x2)
{
   System.out.println("Radio button is aligned");
}
else
{
   System.out.println("Radio button is not aligned");
}
driver.close();
}
}
Example of Login Form of EroomRent.in Application:-
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 EroomRentLogin {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://eroomrent.in/ownerlogin.php");
WebElement txtemail = driver.findElement(By.name("txtEmail"));
txtemail.sendKeys("riteshmahajan1997@gmail.com");
WebElement txtpass = driver.findElement(By.name("txtPassword"));
txtpass.sendKeys("ritesh@123");
driver.findElement(By.name("btnsubmit")).click();
driver.findElement(By.linkText("Logout")).click();
}
}

package com.scs.basic;

import java.sql.Driver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class VerifyLogin {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver scs = new ChromeDriver();
scs.get("https://eroomrent.in");
WebElement ele = scs.findElement(By.xpath("//*[@id=\"collapsibleNavbar\"]/ul/li[3]/a"));
Actions obj = new Actions(scs);
Action ack = obj.moveToElement(ele).build();
ack.perform();
    scs.findElement(By.xpath("//*[@id=\"collapsibleNavbar\"]/ul/li[3]/ul/li[2]/a")).click();
scs.findElement(By.xpath("//html/body/div/div/center/form/input[1]")).sendKeys("riteshmahajan1997@gmail.com");
scs.findElement(By.xpath("//html/body/div/div/center/form/input[2]")).sendKeys("ritesh@123");
scs.findElement(By.xpath("//html/body/div/div/center/form/center/input")).click();
if(scs.getCurrentUrl().equals("https://eroomrent.in/owner/dashboard.php"))
{
System.out.println("Login Successfully");
}
else
{
System.out.println("Login Not Successfully");
}
    scs.close();

}

}

2) Navigate Command 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 WebDriverNavigateExample {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://shivaconceptsolution.com/");
Thread.sleep(2000);
driver.navigate().to("https://shivaconceptdigital.com");
Thread.sleep(2000);
driver.navigate().back();
Thread.sleep(2000);
driver.navigate().forward();
}
}
3) switch to Command:-

It is the switch from one window to another on the same web page
The switch will load a particular container hence we should provide the container name in the 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();
}
}

Another Example  of Switch with Alert, Confirm and Prompt box?


package scs;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HandleAlertExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.shivaconceptsolution.com/backupmain/test3.html");
driver.findElement(By.cssSelector("input[value=alert]")).click();
String s = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println("Result is "+s);
driver.findElement(By.cssSelector("input[value=confirm]")).click();
String s1 = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println("Result is "+s1);
driver.findElement(By.cssSelector("input[value=prompt]")).click();
                driver.switchTo().alert().sendKeys("hi how are you");
String s2= driver.switchTo().alert().getText();
WebDriverWait wait = new WebDriverWait(driver,30);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Type your message
alert.sendKeys("Selenium");
//System.out.println(s3);
//Press the OK button
alert.accept();
// String s3 = driver.switchTo().alert().getText();
System.out.println(s3);
}

}

package webdriverexample;

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

public class GetAttributeExample {

public static void main(String[] args) {
System.setProperty("webdriver.chome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://eroomrent.in");
WebElement imgelement = driver.findElement(By.className("img-fluid"));
        String s = imgelement.getAttribute("src");
        System.out.println(s);
        driver.get(s);
}

}

    

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)