whatsapp

Selenium Intermediate Level Tutorial: To Learn better

Selenium

Friday April 5, 2019

Through our previous blog Selenium Tutorial for Beginners: Overall view into the Tool We gave you the basic info that you need to know about the tool. Now let’s go further and learn much more about this impeccable web app testing tool.

How to upload a file in Selenium test cases?

To upload a file, first, you have to identify the element on which you have to upload your file. There you can directly use sendKeys() function of selenium web driver. You can pass the path of the location in sendKeys. In this way, you will be able to upload a file using selenium web driver.

public static void main(String[] args) {

System.setProperty(“webdriver.gecko.driver”,”path of gecko driver”);

String baseUrl = “http://www.google.com /upload/”;

WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);

WebElement element = driver.findElement(By.id(“id of element”));

uploadElement.sendKeys(“C:\\newhtml.html”);

//Here,  above you have to pass the path of the file where your file is located.

// Then you can click the upload file link

driver.findElement(By.xpath(“”)).click();

}

How to use a web table in selenium script

You have to access a web table and the elements present in the table. You can get it by making  an xpath. Suppose you have having a table with four blocks.

selenium intermediate tutorial
selenium intermediate tutorial

The first thing which you have to do is to find the XPath of the web element in this web table. Let’s say you want to get to the third element in the above web element.

The coding of the above web table is as below:

 

Selenium Intermediate Level TutorialSelenium Intermediate Level Tutorial
Selenium Intermediate Level Tutorial

Now, you can analyze that first there is a table and then there is a tbody. In that tbody there are two rows. One row is having two tables. The first row is having two cells – First and Second. The second row is having two cells – Third and Fourth.

Our goal is to reach to the third cell. Let’s try to make the XPath of it.

The XPath of it will be //table/tbody/tr[2]/td[1]

So, the table is the parent node from which we will iterate to go the third element. From there, we will go to the tbody section and then the second row. From there we will get the first column.

Let’s write a script to get the text out of it.

public static void main(String[] args) {

String url = “http://testsite.com/test/write-xpath-table.html”;

WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);

String txtWebelement = driver.findElement( By.xpath(“//table/tbody/tr[2]/td[1]”).getText();

System.out.println(txtWebelement);

driver.close();

}

}

Let’s take an example of a nested web table. You have to then analyze it carefully and get the XPath of it. Let’s look at the example below to get more information on it.

Selenium Intermediate Level Tutorial:
Selenium Intermediate Level Tutorial:

So, if you want to access the web element which is having text 10-11-12 then you can do it by traversing from the table and then iterating through the rows and columns to reach there.

Xpath would be: //table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[2]

public static void main(String[] args) {

String url = “http://testsite.com/test/write-xpath-table.html”;

WebDriver driver = new FirefoxDriver();

driver.get(baseUrl);

String txtWebelement = driver.findElement( By.xpath(“//table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[2]

”)getText();

System.out.println(txtWebelement);

driver.close();

}

}

This way, you can iterate through the rows and columns to reach to a specific cell from a web table.

Now, one of the most important concepts in selenium which will help you in many cases when you won’t be retrieving any text from a web element or to enable a web element to get the text or to perform any action on it.

Let’s talk about JavaScript Executor in detail. It is an interface which helps to execute javascript.

JavaScript Executor

Sometimes you are not able to click on a web element using click() function. You can then use javascript executor to execute click function on a web element. Let’s have a look at the code.

WebDriver driver= new FirefoxDriver();

// JavascriptExecutor interfaceobject creation by type casting driver object

JavascriptExecutor js = (JavascriptExecutor)driver;

You can now click on a webelement using below command.

WebElement button = driver.findElement(By.xpath(“”));

Js.executeScript(“arguments[0].click();”,button);

Also, if send keys isn’t working. You can make use of java script executor to send keys. Let’s look at the example below.

WebDriver driver= new FirefoxDriver();

// JavascriptExecutor interfaceobject creation by type casting driver object

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(“document.getElementById(‘id’).value=”value;”);

You can even make use of java script executor to refresh a web page. You can do it by following command:

WebDriver driver= new FirefoxDriver();

// JavascriptExecutor interfaceobject creation by type casting driver object

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(“history.go(0)”);

Sometimes, getText() doesn’t work and then you have to make use of java script executor to get text of a web element. You can do it by following line of code:

WebDriver driver= new FirefoxDriver();

// JavascriptExecutor interfaceobject creation by type casting driver object

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript(“history.go(0)”);

Sysout(js.executeScript(“return document.documentElement.innerText;”).toString());

You can even get the title and URL of a web page using java script executor. The procedure is very simple. Let’s have a look at the following lines of code.

WebDriver driver= new FirefoxDriver();

// JavascriptExecutor interfaceobject creation by type casting driver object

JavascriptExecutor js = (JavascriptExecutor)driver;

System.out.println(js.executeScript(“return document.title;”).toString());

System.out.println(js.executeScript(“return document.URL;”).toString());

Desired Capabilities Concept in selenium web driver

You can make the set of configurations on which you want a particular test script to run. You can pass browser name, version to specify the type of environment on which you want a test case to run.

Let’s see some of the capabilities which you can set in a test case for IE browser.

//it is used to define IE capability

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();

cap.setCapability(CapabilityType.BROWSER_NAME, “IE”);

cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

In the above capability, we have passed the capability browser name and we have ignored the security domain.

After setting the capabilities you can pass the capabilities to the web driver instance so that it executes the test on a particular configuration.

Let’s have a look at the complete set of code.

public static void main(String[] args) {

//it is used to define IE capability

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();

cap.setCapability(CapabilityType.BROWSER_NAME, “IE”);

cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);

System.setProperty(“webdriver.ie.driver”, “path of executable”);

WebDriver driver = new InternetExplorerDriver(capabilities);

driver.get(“http://gmail.com”);

driver.quit();

}

Handling a dropdown in selenium web driver

You have to first import following packages: org.openqa.selenium.support.ui.Select.

First, you have to identify from DOM whether the dropdown is of select type or not. If it is of select type then you have to go for the below steps.

Then, you have to uniquely identify the select tag. You have to first make the object of Select class and pass the element for which you have to choose the options from the drop-down.

Select dropdown = new Select(driver.findElement(By.xpath(“”)));

Now, there are three main methods which you have to use to select any element from this select object.

  1. selectByVisibleText
  2. selectByIndex
  3. selectByValue

You can either select any element from this drop down by matching the visible text with that of the text passed by you. You can also select any web element from the drop-down using an index. Last option is to select by value tag.

Also, there are some more functions which are available. Some of them are selectAll() and deselectAll() too select and deselect all the elements when more than one element can be selected.

But, if the dropdown is not of select type then you can’t go for the conventional method. You have to follow another method. You have to uniquely identify the web element on which you have to select a dropdown option. You can identify it and the sendKeys() function of selenium web driver to send keys to the uniquely identified dropdown.

WebElement dropdown = driver.findElement(By.xpath(“”));

Dropdown.sendKeys(“value to be selected”);

How to select the checkbox and radio button is selenium

Sometimes, you come across situations where you have to select checkboxes and radio buttons. You can do it easily with selenium web driver. You just have to use click() function of web driver to click on checkbox and radio button. You can even check of the web element is selected or not.

.isSelected() checks if any web element is selected or not.

It gives false if the web element is not selected and it gives true if the web element is selected.

This way, you can handle radio buttons and checkboxes in a selenium script.

Conclusion

So, now we have studied the entire essential step which would promote you to the intermediate level. You will get a lot of confidence with this stuff.

You can start the practice and make your experience stronger with this course. You can practice all stuff one by and one, and soon you will have a good hands-on it.  All the best!

 

Mail

Hire a Tester

WhatsApp

Call Us

Testbytes