Selenium WebDriver


Create Tests Using Selenium WebDriver
--------------------------------------------
I) Selenium WebDriver setup

II) Element Locators

III) WebDriver Methods

IV) Create Tests using Element locators in addition to WebDriver methods

V) Cross Browser Testing
 

--------------------------------------------
I) Selenium WebDriver setup:

 --------------------------------------------
Details:

Operating Environment: MS Windows 7

Programming Language: Java

IDE: Eclipse
-----------------------------------
> Download Java (JDK) in addition to Install

> Set Path variable (Environment Variable) inwards social club to access Java from all directories inwards C Drive.

> Download Eclipse Software

> Download WebDriver Java Language binding in addition to add together to Eclipse

> Download in addition to Install Firebug in addition to Firepath Add ons for Firefox Browser to inspect spider web elements.
(Note: If it is IE or Chrome, nosotros no require install, developer tools are built in.)
--------------------------------------------------
If nosotros desire to exercise Tests / Test Scripts/ Test cases inwards Selenium WebDriver, ii things are important:

i) Element Locators to identify/recognize spider web elements
(Note: Use unmarried unique locater to recognize spider web elements)

ii) WebDriver Methods to perform operations on Web Elements
---------------------------------------------
iii) Programming features similar Conditional statements, Loop statements, Methods, variables, operators etc... to heighten Test cases.
--------------------------------------------
II) Web Element Locators:

 --------------------------------------------
Selenium WebDriver uses 8 chemical ingredient locators to detect elements on Web pages.

Why nosotros require to role dissimilar locators?

i) Developers may non render all locaters for all elements

ii) Some locaters may survive duplicated.

So nosotros convey to pick out whatsoever ane unique locater to recognize the element.
-----------------------------------------
1) id

Syntax:

By.id("id value")

Examples:

driver.findElement(By.id("Email"))

driver- is Object

findElement - WebDriver method

By - pre-defined Class

id - Elment locater

Email - id locator value
-------------------------------------
driver.findElement(By.id("Email")).sendKeys("gcrindia");
--------------------------------------------
id locator for Edit box

WebElement Email = driver.findElement(By.id("Email"));
Email.sendKeys("gcrindia");
-------------------------------------------
id locator for Button

WebElement Email = driver.findElement(By.id("signIn"));
Email.click();

Or

driver.findElement(By.id("signIn")).click();
-------------------------------------------------------
2) name

Synatx:

By.name("name value/locator name")       

Examples:

driver.findElement(By.name("Email")).sendKeys("gcrindia");
       
Or

WebElement e = driver.findElement(By.name("Email"));
e.sendKeys("gcrindia");
------------------------------------------
WebElement e = driver.findElement(By.name("signIn"));
e.click();
------------------------------------------
3) className

Syntax: By.className("class mention value")

Example:

driver.findElement(By.className("textboxcolor")).sendKeys("Hyderabad");
----------------------------------------------
4) tagName

Syntax:
By.tagName("tag mention value")

Example:
driver.findElement(By.tagName("input")).sendKeys("Hyderabad");
--------------------------------------------------------------       
5) linkText

Syntax:
By.linkText("Link Text Value")

Example:

driver.findElement(By.linkText("Gmail")).click();
-----------------------------------------------------               
6) paritialLinkText

Syntax:
By.partialLinkText("Partial Link Text Value")

Example:

driver.findElement(By.partialLinkText("Gma")).click();
-----------------------------------------------
7) cssSelector

Syntax:
By.cssSelector("value")

Example:
driver.findElement(By.cssSelector(".gb_m")).click();
---------------------------------------------------
8) xpath

Syntax:

By.xpath("xpath value")

driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("abcdef");
--------------------------------------------
III) WebDriver Methods 

--------------------------------------------
1) larn Method()

It opens specified URL inwards the Browser window

Syntax:
driverObject.get("URL")

Example:
driver.get("http://www.google.com");
--------------------------------------------
2) getTitle Method()

It returns championship of the Browser

Syntax:
String stringName = driverObject.getTitle();

Example:
String Title = driver.getTitle();
System.out.println(Title);
--------------------------------------------
3) getPageSource()

It returns the page source

Syntax:
String stringName = driverObject.getPageSource();

Example:
String Pagesource = driver.getPageSource();
System.out.println(Pagesource);
--------------------------------------------
4) getCurrentUrl()

It returns electrical flow url of the Browser

Syntax:
String stringName = driverObject.getCurrentUrl();

Example:
String CurrentUrl = driver.getCurrentUrl();
System.out.println(CurrentUrl);
--------------------------------------------
Browser Navigation Methods:

5) navigate().to() Method

Loads a novel spider web page inwards the electrical flow browser window.

Synatx:

driverObject.navigate().to();

example:
driver.navigate().to("http://www.yahoo.com");
------------------------------------------
6) navigate().back();

It moves a unmarried especial dorsum inwards the spider web browser's history

Syntax:

driverObject.navigate().back();
---------------------
Example:

driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().back();
--------------------------------------------
7) navigate().forward()

It moves a unmarried especial forrard inwards the Browser history

Syntax:
driverObject.navigate().forward();

Example:
driver.get("http://www.google.com");
driver.navigate().to("http://www.yahoo.com");
driver.navigate().back();
driver.navigate().forward();//yahoo
--------------------------------------------
8) naviagte().refresh()

It refreshes the electrical flow spider web page

Syntax:
driverObject.navigate().refresh();

Example:
driver.get("http://www.google.com");
driver.navigate().refresh();
--------------------------------------------
9) close()

It closes the foused browser

Synatx:

driverObject.close();

Example:

driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.close();
--------------------------------------------
10) quit()

It closes all Browsers that opened yesteryear WebDriver during execution.

Synatx:

driverObject.quit();

example:
driver.get("file:///E:/HTMLExamples/LoginPage.html");
driver.findElement(By.linkText("Sign In")).click();
driver.quit();
--------------------------------------------
11) findElement()

It finds root chemical ingredient inside the electrical flow page using the given locator in addition to returns unmarried element.

Syntax:

WebElemnt objectname = driver.findElement(By.locator("locator value"));
objectname.method();

Or
driver.findElement(By.locator("locator value")).method();

Example:
WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");

Or
driver.findElement(By.id("Email")).sendKeys("abcd");
--------------------------------------------
12) sendKeys()
Enters a Value inwards to Text box / Edit box

Syntax:

driverObject.findElement(By.lcator("locator value")).sendkeys("value");

Or

WebElement ObjectName = driverObject.findElement(By.lcator("locator value"))
ObjectName..sendkeys("value");

Example:
driver.findElement(By.id("Email")).sendKeys("abcd");

Or

WebElement e = driver.findElement(By.id("Email"));
e.sendKeys("abcd");
--------------------------------------------
13) clear()
It clears the value from a text box or edit box

Syntax:
driverObject.findElement(By.lcator("locator value")).clear();

Example:
driver.findElement(By.id("Email")).clear();
--------------------------------------------
14) click()
It clicks an element

Synatx:
driverObject.findElement(By.locator("locator value")).click();

Or
WebElement ObjectName = driverObject.findElement(By.locator("locator value"));
ObjectName.click;

Examples:
click method on Button Object

driver.get("http:/gmail.com");
driver.findElement(By.id("signIn")).click();
---------------------------
click method on Link Object

driver.get("http:/google.com");
driver.findElement(By.linkText("Gmail")).click();
--------------------------------------------
15) isEnabled()
It checks conditions the chemical ingredient is inwards enabled country or not?

Syntax:

boolean variableName = driverObject.findElement(By.locator("locator value")).isEnabled();

Example:
driver.get("http:/google.com");
boolean i = driver.findElement(By.linkText("Gmail")).isEnabled();
System.out.println(i);
--------------------------------------------
16) maximize()
Syntax:
driver().manage().window().maximize();

Example:
driver.get("http:/google.com");
driver.manage().window().maximize();
--------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post