5 Selenium tips to make your life easier

You may already be using Selenium WebDriver as the main tool for solving web automation tasks. It is indeed one of the most popular frameworks in the field of automated testing. In this article, we decided to share five ways that you probably did not know about, and how they can be useful in your test scenarios with selenium.

Using the information provided below, testers can easily organize their tests, track their progress, and generate detailed reports. These techniques will make the testing process more organized and efficient.

Method #1 – Running multiple tests with the same parameters

This technique is especially useful when browser automationwhen different tests need to use the same values. To do this, you can use the capabilities of TestNG and set specific parameters for the test suite in the configuration file.

To give an example, the code specifies the URL for the WebDriver method to execute the test suite. These tests are implemented within TestNG framework.

public class TestNGParameterExample {
@Parameters({"firstSampleParameter"})
@Test(enabled = true)
public void firstTestForOneParameter(String firstSampleParameter) {
driver.get(firstSampleParameter);
WebElement searchBar = driver.findElement(By.linkText("Mobile Device & Browser Lab"));
searchBar.click();
Assert.assertEquals(driver.findElement(By.tagName("h1")).getText(), "Mobile Device & Browser Lab");
}
@Parameters({"firstSampleParameter"})
@Test
public void secondTestForOneParameter(String firstSampleParameter) {
driver.get(firstSampleParameter);
WebElement searchBar = driver.findElement(By.linkText("Live Cross Browser Testing"));
searchBar.click();
Assert.assertEquals(driver.findElement(By.tagName("h1")).getText(), "Cross Browser Testing");
}
}
testng.xml file structure is shown below.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<parameter name="firstSampleParameter" value="https://experitest.com/"/>
<test name="Nopackage" >
<classes>
<class name="TestNGParameterExample" />
</classes>
</test>
</suite>

Remember that all parameterized tests must be initiated with a builder such as, for example, Maven.

Method #2 – Passing simple values ​​to the test

In this case, the goal is to create tests that allow you to process data sets (datasets). For this, the DataProvider method is used, which can return a two-dimensional array of objects. You can represent a two-dimensional array of objects as a table, where:

  1. The first dimension of the array indicates the number of different test run scenarios.

  2. The second dimension describes how many different test values ​​or parameters are used in each scenario.

Thus, you create a test plan with different test cases, where each case can include different test cases or conditions. It helps to organize and structure your work and analyze the behavior of your code in different situations.

For a better understanding, consider the code example below, which uses the @DataProvider annotation.

@DataProvider()
public Object[][] listOfLinks() {
return new Object[][] {
{"Mobile Device & Browser Lab", "Mobile Device & Browser Lab"},
{"Live Cross Browser Testing", "Cross Browser Testing"},
{"Automated Mobile Testing", "Mobile Test Automation!"},
};
}

The code below refers to a data provider method with the appropriate name.

@Test(dataProvider = "listOfLinks")
public void firstTestWithSimpleData(String linkname, String header) {
driver.get("https://experitest.com/");
WebElement searchBar = driver.findElement(By.linkText(linkname));
searchBar.click();
Assert.assertEquals(driver.findElement(By.tagName("h1")).getText(), header);
}

Method number 3 – Handling pop-ups

A common issue with Selenium web automation is interacting with popups. These windows are usually of three types:

  1. Simple alert: displays a message

  2. Confirmation Alert: prompts the user for confirmation of some operation

  3. Alert hints: informs the user to enter some data

While WebDriver cannot drive Windows-based alerts, it can handle them in web pages. To manage pop-up windows, use the method switch_to.

<!DOCTYPE html>
<html>
<body>
<h2>
Demo for Alert</h3>

Clicking below button 
<button onclick="create_alert_dialogue()" name ="submit">Alert Creation</button>
<script>
function create_alert_dialogue() {
alert("Simple alert, please click OK to accept");
}
</script>
</body>
</html>

Below is an example using the method switch_to. In this case switch_to.alert used to navigate to the alert popup window. After that, the alert is received by the method alert.accept().

from selenium import webdriver
import time
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from builtins import str

driver = webdriver.Firefox()
driver.get("file://<HTML File location>")

driver.find_element_by_name("submit").click()
sleep(5)

alert = driver.switch_to.alert
text_in_alert = alert.text
print ("The alert message is "+ text_in_alert)
sleep(10)

alert.accept()

print("Alert test is complete")

driver.close()

Method #4 – Working with dynamic content

Modern websites often surprise us with their dynamics and volatility. This is especially true for applications that use AJAX technology.

Consider, for example, online shopping. These sites are often tailored to each user, showing products based on location or the customer’s previous choices. And, when we test sites like this, it’s important to make sure that WebDriver continues to run when all page elements have fully loaded.

Problems can arise if elements on dynamic pages appear at different times and WebDriver tries to access them when they do not yet exist in the document structure. But here they will help us wait commands in Selenium.

With Explicit Wait, a tester can tell WebDriver to pause tests until a certain condition is met. And if waiting for a certain period of time is required, for this we can use the function thread.sleep().

I will give an example below. Let’s say we have two searches on the site https://www.browserstack.com/. First search for an element called home-btn. We find it using the attribute CLASS_NAMEbut not more than 5 seconds.

The second search is for the clickable element login, and again, we wait no more than 5 seconds. If the element appears, then the action is performed on it click().

In both cases, we use WebDriverWait along with Expected Condition.

from selenium import webdriver
from time import sleep
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.get("https://www.browserstack.com/")

try:
myElem_1 = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'home-btn')))
#element = driver.find_element_by_partial_link_text("START TESTING")
print("Element 1 found")
myElem_2 = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')))
print("Element 2 found")
myElem_2.click()
sleep(10)
#except NoSuchElementException:
except TimeoutException:
print("No element found")

sleep(10)

driver.close()

Method number 5 – Working with drop-down menus

Let’s start by understanding the basics of this topic.

To work with dropdown menus in Selenium WebDriver, use the class Select. It allows selection and deselection of options in a drop-down menu. To create objects of type Select, they must be initialized by passing the drop-down menu element (WebElement) as a parameter to the constructor.

Here is an example of usage:

WebElement testDropDown = driver.findElement(By.id("testingDropdown")); 
Select dropdown = new Select(testDropDown);

There are three ways to select an option from a drop-down menu:

1. selectByIndex – allows you to select an option by index, starting from 0.

dropdown.selectByIndex(5);

2. selectByValue – used to select an option based on its ‘value’ attribute.

dropdown.selectByValue("Database");

3. selectByVisibleText – is used to select an option based on the text above it.

dropdown.selectByVisibleText("Database Testing");

Use the methods above to make your Selenium tests more convenient, organized and less time consuming. In addition, it is important here to choose a Selenium-based platform that will facilitate automated testing. Pay attention to the tools that allow you to test in the cloud on real devices, such as BrowserStack. This tool is designed to make life easier for testers by providing developer-focused resources such as built-in development tools and integration with popular programming languages ​​and frameworks. Thus, automated testing using Selenium becomes faster, smoother and more comfortable, results-oriented.


In conclusion, we invite everyone interested to an open lesson on the topic “Java Generics and their role in automation.” You can sign up for a lesson on the course page “Java QA Engineer. Professional”.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *