Setup Firebug, Fire Path, XPath Checker

Setup Firebug, Fire Path, XPath Checker

Selenium script is nothing but identify elements (text,text box, links, buttons, checkbox, windows etc.) on webpage and perform operations in order to automate it.
So those elements are identified by locators, (will explain locators in next topic). And above mentioned tools are played important role to identify locators, so install it first.

1.Firebug Installations:

Firebug is Add-on to Firefox browser.

Open Firefox browser–>Tools–>Add-ons–>Extensions–>search for Firbug–>Install

install Firbug for selenium webDriver

Once you install firebug, you will see firebug icon in Firefox browser at top right corner (see in image bellow)

How to use Firebug in Selenium WebDriver

2.FirePath Installation:

Firebug is Add-on to Firebug,Installation process is same like firebug.

Open Firefox browser –>Tools–>Add-ons–>Extensions–>search for Firpath–>Install

install FirePath for selenium webDriver

Once you install Firepath, Restart the Firefox browser, open any website any open Firbug.
In Firebug panel you can see Firpath , from where you can get readymade Xpath which you can use in script ahead.

How to use Firpath for selenium webDriver

3.XPath Checker Installation:

XPath Checker is Addon to Firefox. Installation process is same like firebug

Open Firefox browser –>Tools–>Add-ons–>Extensions–>search for Xpath Checker–>Install

install XPath Checker for selenium webDriver

Once you install XPath Checker ,restart Firefox browser. Open any website in Firefox ,right click on any web element, you can see View xpath option, means Xpath checker is installed.
Click on it, xpath checker window will appear, where you can identify xpath manually.

How to use Xpath Checker for selenium webDriver

Get Tooltip Text Using Selenium WebDriver

Get Tooltip Text

Tooltip : many times we see, when we hovers mouse on button or link or text or any web page field,an information displays; is nothing but tooltip! With respect to text validation test cases ,it is must to verify whether tooltip text is displayed correctly or not.
So,Let us see how to get tooltip text using selenium webdriver.
check below image and selenium code for the same.

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class tooltip {

public static void main(String[] args) throws InterruptedException {

//set GeckoDriver path for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Create Browser object
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("[http://www.seleniumpoint.com/testwebsite.php](http://www.seleniumpoint.com/testwebsite.php)");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(3000);

//Get Tool tip text : mouse hover on product 1 name : Nikon d 50 you will see tooltip

String toolTipText = driver.findElement(By.xpath("//div[@class='product1Name']//a")).getAttribute("title").toString();
System.out.println("Tool Tip Text is :" + toolTipText);

}

}

Print Table Data Using Selenium WebDriver

Selenium : Print Table Data

We can find every web element using its tag name. table has tr tag for row.
In Selenium WebDriver we can use driver.findElements(By.tagName(“tr”) to find all rows data.
Check below image and Selenium code for the same:

get table data using selenium webdriver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class printTableData {

public static void main(String[] args) throws InterruptedException {

//set GeckoDriver path for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Create Browser object
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(3000);

//Get Row Count
int rowCount = driver.findElements(By.tagName("tr")).size();

//Get Column Count
int colCount = driver.findElements(By.xpath("//thead//th")).size();

System.out.println("Row count :" + rowCount);
System.out.println("Col count :" + colCount);

//Print table Data

for (WebElement tdata: driver.findElements(By.tagName("tr"))) {
System.out.println(tdata.getText());
}
}

}

Print All Web Page Links using Selenium WebDriver

We can find every web element using its tag name. Links has anchor tag (a).
In Selenium WebDriver we can use driver.findElements(By.tagName(“a”) to find all links on web page
Run the below Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package scripts;

import org.openqa.package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class printAllLinks {

static int linkCount = 0;

public static void main(String[] args) {

//set GeckoDriver path for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//Create Browser object
WebDriver driver = new FirefoxDriver();

//Open URL
driver.get("[http://www.seleniumpoint.com](http://www.seleniumpoint.com/)");

//Get links in for each loop

for (WebElement links: driver.findElements(By.tagName("a"))) {
System.out.println(links.getText());
linkCount++;
}

//Print Link Count

System.out.println("Total Links on Page : " + linkCount);

}

}

Alerts Handling using Selenium WebDriver

Alerts Handling using Selenium WebDriver

For handling alerts on web page, Selenium provides four methods:

  • 1. accept() To accept the alert on web page
  • 2. dismiss() To dismiss the alert on web page
  • 3. getText() To get the text of the alert on web page
  • 4. sendKeys() To send some text to the alert on web page

Find below image and selenium code for the same

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package scripts;

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.firefox.FirefoxDriver;


public class alertHandling {

public static void main(String[] args) throws InterruptedException {

//set Chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(3000);

//click on View link of third product to open window: Car Seat
driver.findElement(By.xpath("//div[@class='product4View']/a")).click();

Thread.sleep(3000);

//get alert text
String alertText = driver.switchTo().alert().getText();
System.out.println("Alert Text is :" + alertText);

//Send value to alert
driver.switchTo().alert().sendKeys("01harshal.shewale@gmail.com");

//accept alert
driver.switchTo().alert().accept();

//dismiss alert
//driver.switchTo().alert().dismiss();


}

}

Frame Handling Using Selenium WebDriver

Selenium : Frame Handling

Same like window, for automating the content in frame we have to use switchTo().frame(“frameName”);
(with given example below, frame is situated in new window, so we must have to switch to window first and then switch to frame to perform operations.)
Run the below Code:

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package scripts;

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

/*
* Purpose of script is click on AddToCart button in new frame
* Here frame is situated in new window , so first we have to switch to window and switch to frame
*/

public class seleniumFrameHandling {

public static void main(String[] args) throws InterruptedException {

//set chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(4000);

//First Get parent window,means current window handle
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window Title " + driver.getTitle());

//click on View link of third product to open window: Car Seat
driver.findElement(By.xpath("//div[@class='product3View']/a")).click();

//Now here is the New Tab handling code

//Get All Tabs or Window handles and iterate using for each loop
for (String handle: driver.getWindowHandles()) {
System.out.println(handle);
driver.switchTo().window(handle);

}

//switch to frame using frame name
driver.switchTo().frame("carseatframe");

//Click on Cart Button
driver.findElement(By.xpath("//button")).click();

//Go back to Parent window
driver.switchTo().window(parentWindow);



}

}

New Tab Window handling using Selenium WebDriver

Selenium : New Tab Window handling

Tab is same like window. There is no difference with respect to selenium. There is difference at code level, I mean HTML code level. But for selenium automation both are the same.So, automating the new tab content is same like window.
You have to use switchTo().window()
Run below code for the same.

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package scripts;

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

/*
* Purpose of script is click on AddToCart button in new tab
*
*/

public class newTabHandling {

public static void main(String[] args) throws InterruptedException {

//set chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(4000);

//First Get parent window,means current window handle
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window Title " + driver.getTitle());

//click on View link of second product to open window: Bag
driver.findElement(By.xpath("//div[@class='product2View']/a")).click();

//Now here is the New Tab handling code

//Get All Tabs or Window handles and iterate using for each loop
for (String handle: driver.getWindowHandles()) {
System.out.println(handle);
driver.switchTo().window(handle);

}

//Click on Cart Button
driver.findElement(By.xpath("//button")).click();

//Go back to Parent window
driver.switchTo().window(parentWindow);
}

}

Window handling using Selenium WebDriver

Selenium : Window handling

Many times on web application we see contents opens in window. So, for automating the newly opened window using selenium, we must have to switch to that window.
Here is a code for the same. You can try this code by yourself. Just copy the code below and run locally using eclipse.

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package scripts;

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

/*
* Purpose of script is click on AddToCart button in new Window
*
*/

public class windowHandling {

public static void main(String[] args) throws InterruptedException {

//set chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//login to test web site
driver.findElement(By.id("remail")).sendKeys("test@seleniumpoint.com ");
driver.findElement(By.id("rpassword")).sendKeys("test@123");
driver.findElement(By.id("rsubmit")).click();

Thread.sleep(4000);

//First Get parent window,means current window handle
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window Title " + driver.getTitle());

//click on View link of first product to open window: Nikon D50
driver.findElement(By.xpath("//div[@class='product1View']/a")).click();

//Now here is the Window handling code

//Get All windows handles and iterate using for each loop
for (String handle: driver.getWindowHandles()) {
System.out.println(handle);
driver.switchTo().window(handle);

}

//Click on Cart Button
driver.findElement(By.xpath("//button")).click();

//Go back to Parent window
driver.switchTo().window(parentWindow);
}

}

Select Dropdown values using Selenium WebDriver

Select Dropdown values

Selenium WebDriver provides n number of inbuilt methods to perform actions on appliaction under test. For selecting dropdown values, selenium provides below three methods:

  • 1. selectByIndex
  • 2. selectByValue
  • 3. selectByVisibleText

selectByIndex means select dropdown value using its index. Index starts from zero.
for selectByValue and selectByVisibleText , inspect using firebug to see its value and visible text.(Value and visible text can be same or different)
Refer below image and selenium code for the same

selenium webdriver window handling

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package scripts;

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.support.ui.Select;

public class selectDropdownValues {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

//set chromedriver path for Chrome browser
System.setProperty("webdriver.chrome.driver", "C:\\harshal personal\\selenium\\chromedriver.exe");

//create WebDriver object for Chrome
WebDriver driver = new ChromeDriver();

//open URL
driver.get("https://www.seleniumpoint.com/testwebsite.php");

//maximize the window
driver.manage().window().maximize();

//get the Locator of drop down box
WebElement dropdown = driver.findElement(By.name("ustate"));

//Create object of Select Class
Select select = new Select(dropdown);

//We can select Drop down value by three ways:

//Method 1: Select drop down value using index
select.selectByIndex(4);

Thread.sleep(2000);

// Method 2: Select drop down value using index
select.selectByValue("Chhattisgarh");

Thread.sleep(2000);

//Method 3: Select drop down value using index
select.selectByVisibleText("Maharashtra");


}

}

Selenium WebDriver Wait

Selenium WebDriver Wait

Most of the web application are using AJAX techniques. When a page is loaded to browser, the elements within that page may load at different time intervals. This makes locating elements difficult, if the element is not present in the DOM, it will raise exceptions like ElementNotVisibleException, ElementNotPresentExceptionetc.
Using waits, we can solve this issue.
So, let us discuss about different selenium waits available in Selenium WebDriver:

1.Implicit Wait

It is used to set default wait time between two steps. For example, if you want to put wait for 30 seconds, use implicit wait like below:

1
2
3
4
5
6
7
8
9
10
11
//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();

//Set Implicit wait for 30 second
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//open URL
driver.get("https://www.seleniumpoint.com/");

//Perform further actions....
driver.findElement(By.id("name")).sendKeys("any value");

In the above example the implicit waits value is given as 30sec i.e, if web driver able to find the element with in the given span 30sec it immediately returns true and go to next step else it raises an exception after 30 seconds.

IMP: Implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.

2.Explicit Wait

Explicit waits are used to pause the execution till the time a particular condition is occur or the maximum time has elapsed.Explicit waits are applied for a particular instance only.
WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.
For example:
If you are writing a script for login to Gmail account, you will enter username , password and click on Sign In button. Then wait till the Compose button appearing on next page. This is nothing but explicit wait.

Selenium Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWait {

public static void main(String[] args) throws InterruptedException {

//set Gecko driver for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("https://www.gmail.com/");

//enter username
driver.findElement(By.id("Email")).sendKeys("harshaljshewale@gmail.com");

//click next
driver.findElement(By.id("next")).click();

//Explicit wait till SignIn Button visible
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("signIn"))));

//enter password
driver.findElement(By.id("Passwd")).sendKeys("YourPwd@xyzcom");

//click on Sign In button
driver.findElement(By.id("signIn")).click();

//Explicit wait till Compose Button to be clickable
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='z0']")));

driver.findElement(By.xpath("//div[@class='z0']")).click();


}

}

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×