You are on page 1of 4

SELENIUM

Why we write in Selenium Scripts - WebDriver driver = new


ChromeDriver()
Interfaces:

· WebDriver
Classes:

· ChromeDriver
· FirefoxDriver
· InternetExplorerDriver
Explanation:

· WebDriver is an interface in selenium, which defines common methods which all


browser classes use (such as Firefox, Chrome etc.,).
· All the abstract methods of WebDriver interface are implemented inRemote WebDriver
class and it is extended by all the browser related classes such as ChromeDriver,
FirefoxDriver, InternetExplorerDriver etc.

Why not WebDriver driver = new WebDriver ()


We cannot write our code like this because we cannot create Object of an Interface. WebDriver is an
interface.

Why we won’t prefer ChromeDriver driver = new ChromeDriver ()


The ChromeDriver instance which gets created based on above statement will be only able to invoke
and act on the methods implemented by ChromeDriver and supported by Chrome Browser only. We
know that ChromeDriver is a class and it has all the methods of WebDriver interface. Using this
statement, we can run our scripts only on Chrome Browser.

WebDriver driver = new ChromeDriver ()


We can create Object of a class ChromeDriver by taking reference of an interface (WebDriver). In
this case, we can call implemented methods of WebDriver interface. Selenium developers don’t
know how all these browsers work. So Selenium developers just declare methods whatever they
required and leave the implementation part to the browser developers.

Basis methods of WebDriver:


Get() - Load a new web page in the current browser window.
getCurentURl() - Get a string representing the current URL that the browser is
looking at.
getTitle() - The title of the current page
getPageSource() - Get the source of the last loaded page.
Quit() - Quits this driver, closing every opened window by selenium.
Close() - Close the current window, if it's the last window currently open.

Identification of Element by Web Driver


There are 8 different element locators to identify Elements. They are as follows.
1. ID.

2. Name.

3. X-Path.

4. CSS Selectors.

5. Link text.

6. Class name

7. Tag name.

8. Partial Link text.

Element identification in selenium.


· If any element has name or id property we can use the value of id or name property to
identify the element.
· If the element is a link we can use the link text to identify it. <a
· If the element does not have id or name property and if it is not a link then we need to
write either Css selector or X-path expression.
· Elements are generally identified by its html tag, its properties and values. To identify an
element, we should be able to inspect the html source of the element. By default each
browser provides an inspector tool.(F-12 Inspect element in all browser)

How to Write X-path and Css manually


<input type="text" name="email" value placeholder="E-Mail Address" id="input-email"
class="form-control" xpath="1">
Tagname – input
Attribute - type ; value- text
Attribute - name; value- email
Attribute - value placeholder ; value: E-Mail Address
Attribute- id; value: input-email
Attribute – class; value: form-control

Xpath:
//tagName[@attribute='value'] - xpath syntax
//input[@id='input-telephone']

//*[@attribute=’value’] – Regular expression X-Path for tagname


//tagName[contains(@attribute,'value')] - x-path regular expression
Difference between Absolute X-path and Relative X-path
Relative X-path: Whenever we find x-path without depending on the Parent, then it is called
as Relative X-path
Absolute X-path: Starting from the top and reading to our desired nodes is called absolute
x-path
*Relative x-path is preferred upon absolute x-path (as absolute x-path can change)

Excercise:

package Automation;

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

public class LoginRefdifmail {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "E:\\Testing Session\\SeleniumTraining\\


BrowserDrivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//driver.findElement(By.id("login1")).sendKeys("anees");
//driver.findElement(By.id("password")).sendKeys("test12345");
//driver.findElement(By.name("proceed")).click();

//X-path
driver.findElement(By.xpath("//*[@type='text']")).sendKeys("anees");
driver.findElement(By.xpath("//input[contains(@name,'pass')]")).sendKeys("test1234");
driver.findElement(By.xpath("//input[@title='Sign in']")).click();

}
<input type="text" id="login1" maxlength="100" name="login" value="" tabindex="1">

<input type="password" id="password" name="passwd" value="" tabindex="2">

<input type="submit" name="proceed" value="Sign in" title="Sign in" tabindex="4"


class="signinbtn">

XPATH:
//tagName[@attribute='value']

//input[@type='text']

//input[@id='password']

//input[@title='Sign in']

//*[@attribute=’value’] – Regular expression X-Path for tagname


//tagName[contains(@attribute,'value')] - x-path regular expression

Multi Browser code


System.setProperty("webdriver.chrome.driver", "E:\\Testing Session\\SeleniumTraining\\
BrowserDrivers\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("webdriver.gecko.driver", "E:\\Testing Session\\SeleniumTraining\\
BrowserDrivers\\geckodriver-v0.31.0-win64\\geckodriver.exe");
//System.setProperty("webdriver.ie.driver", "E:\\Testing Session\\SeleniumTraining\\
BrowserDrivers\\IEDriverServer_x64_4.0.0\\IEDriverServer.exe");
WebDriver driver = new ChromeDriver();
//WebDriver driver = new FirefoxDriver();
//WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.icicibank.com");
String name = driver.getCurrentUrl();
if(name.equals("https://www.icicibank.com/")) {
System.out.println("Pass");
}
else
{
System.out.println("fail");
}

System.out.println(driver.getTitle());;
driver.close();

You might also like