Handling Elements in Selenium WebDriver

Handling Elements in Selenium WebDriver

Pre-requisites to create Test cases in Selenium

i) Element Locators (To recognize/identify Elements)

ii) WebDriver Methods (To perform operations on Elements)

iii) Programming features (To enhance Test cases)
——————————————–
iv) TestNG Annotations (Grouping Test Cases, Test Batch execution and generating reports.)

i) Element Locators

1) id

2) name

3) className

4) tagName

5) linkText

6) parialLinkText

7) cssSelector

8) xpath

Web Elements

Browser -driver object
Page
———–
Link
Button
Image, Image Button, Image Link
Edit box
Text Area
Check box
Radio Button
Drop down box
List box
Combo box
Web Table / HTML Table
Frame etc…

ii) WebDriver Methods

a) Methods on Browser
1) get()

2) getTitle()

3) getPageSource()

4) getCurrentUrl()

5) getwindowHandle()

6) close()

7) quit()
——————–
b) Browser navigation Methods
1) navigate().to()

2) navigate().back()

3) navigate().forward()

4) navigate().refresh()
——————————
c) Methods on Elements
1) findElement()

2) findElements()

3) sendkeys()

4) clear()

5) click()

6) isEnabled()

7) isDisplayed()

8) isSelected()

9) getText()

10) getAttribute()
———————————–
d) Others
1) manage.window.maximize()

2) .explicitlyWait()

iii) Java Programming Features

A) Java Fundamentals

1) Comments

2) Data Types

3) Modifiers

4) Variables

5) Operators

6) Conditional Statements

7) Loop Statements

8) String handling

9) Arrays in Java

10) Built in Methods

11) User defined Methods

12) Input and Output Operations, File Handling

13) Exception Handling

B) Java OOPS

1) Inheritance

2) Polymorphism

3) Abstraction

4) Encapsulation

Handling Web Elements in Selenium

Handling Elements in Selenium

i) Handling Browser

Operations on Browser:

> Launch the Browser
> Navigate to specified URL
> Return Current URL
> Get the Page Title
> Return Page source
> Return Window handle
> Close focused Browser
> Close all browsers that opened by selenium WebDriver during execution
———–
> Navigate to another URL
> Navigate back to previous URL
> Navigate forward
> Refresh the Browser
> Maximize the Browser window.
————————————-
Examples:

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.google.co.in”);

String PageTitle = driver.getTitle();
System.out.println(PageTitle);

String URL = driver.getCurrentUrl();
System.out.println(URL);

String PageSource = driver.getPageSource();
System.out.println(PageSource);

String WindowHandle = driver.getWindowHandle();
System.out.println(WindowHandle);

driver.close();
driver.quit();
—————————————-
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(“https://www.google.co.in”);
driver.navigate().to(“https://login.yahoo.com/”);
System.out.println(driver.getCurrentUrl());

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

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

driver.manage().window().maximize();
——————————————–
ii) Handling Edit box

Operations on Edit box

> Enter a value
> Clear the Value
> Return the Value
> Check Displayed status
> Check Enabled statues

Examples:

//Finding Edit box using id locator
driver.findElement(By.id(“Email”)).sendKeys(“gcrindia”);

//Finding Edit box using name locator
driver.findElement(By.name(“Email”)).sendKeys(“gcrindia”);

//Finding Edit box using CSS Selector locator
driver.findElement(By.cssSelector(“#Email”)).sendKeys(“gcrindia”);

//Finding Edit box using xpath locator
driver.findElement(By.xpath(“.//*[@id=’Email’]”)).sendKeys(“gcrindia”);

//Clear the Value
driver.findElement(By.xpath(“.//*[@id=’Email’]”)).clear();
——————————–
WebElement Email = driver.findElement(By.xpath(“.//*[@id=’Email’]”));

Email.sendKeys(“gcrindia”);

//Return Type of the Object
Email.getAttribute(“type”);

//Return the Value
System.out.println(Email.getText());//gcrindia

//Return Displayed status
System.out.println(Email.isDisplayed());//true

//Return Enabled status
System.out.println(Email.isEnabled());//true

//Clear the value
driver.findElement(By.id(“Email”)).clear();
———————————————
iii) Handle Text Area

Capture Text Area/Capture Error Message

Capture Text Area:

driver.get(“https://www.gmail.com”);
String s = driver.findElement(By.xpath(“html/body/div[1]/div[2]/div[1]/h1”)).getText();
System.out.println(s);

Capture Error Message:

WebDriver driver = new FirefoxDriver();
driver.get(“https://login.yahoo.com/”);
driver.manage().window().maximize();
driver.findElement(By.xpath(“.//*[@id=’login-signin’]”)).click();
String ErrorMessage = driver.findElement(By.id(“mbr-login-error”)).getText();
System.out.println(ErrorMessage);

Handle Window Popup

WebDriver driver = new FirefoxDriver();
driver.get(“https://mail.rediff.com/cgi-bin/login.cgi”);
driver.findElement(By.name(“proceed”)).click();

Alert alert = driver.switchTo().alert();
String Error_Message =alert.getText();//Returns Error message
System.out.println(Error_Message);

alert.accept();//Closes OK Button
driver.findElement(By.id(“login1”)).sendKeys(“Inda123”);
———————————
iv) Handle Button

Operations on Button:

> Click
> Check the Displayed status
> Check the Enabled status
> Return name of the Object
> Return type of the Object

Examples:

WebDriver driver = new FirefoxDriver();
driver.get(“https://www.gmail.co.in”);

System.out.println(driver.findElement(By.id(“next”)).isEnabled());//true

driver.findElement(By.id(“next”)).click();

System.out.println(driver.findElement(By.id(“next”)).isDisplayed());//true

System.out.println(driver.findElement(By.id(“next”)).isEnabled());//false

System.out.println(driver.findElement(By.id(“next”)).getAttribute(“type”));

System.out.println(driver.findElement(By.id(“next”)).getAttribute(“name”));

System.out.println(driver.findElement(By.id(“next”)).getAttribute(“value”));
——————————-

(379)

You may also like...

Leave a Reply