We can check if DOM has a class using Selenium webdriver. We can use the findElements method to obtain the list of elements having a particular class. Then pass By.className or By.xpath or By.cssSelector as a parameter to the method.The class name we want to search is passed as a parameter to that method. Let us investigate the below html code for an ul element having class value as toc chapters. Then obtain its sub-elements.Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ClassAttrb{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", ... Read More
There are differences between executeAsyncScript and executeScript methods. For an executeScript method, JavaScript Executor runs the JavaScript with the reference to the present selected window or frame. The script within the method shall run as the body of the unnamed function.Inside the script, the documents are used to point to the present document. Also, the local variables will not be present as the script has completed execution. However, the global variables shall be present.If the script consists of a return statement, then the below rules are followed −A webelement is returned for an HTML element.A double is returned for a ... Read More
We can make Selenium wait for the page to load. We can use the synchronization concept in Selenium to wait for page loading. The implicit wait is a type of synchronization applied to elements to wait for a specified amount of time.Syntaxdriver.manage().timeouts().implicitlyWait();We can also call the JavaScript method document.readyState and wait till it yields the value complete. Selenium executes JavaScript command with the help of the executeScript method.SyntaxJavascriptExecutor j = (JavascriptExecutor)driver; j.executeScript("return document.readyState").toString().equals("complete");Post this step, we should check if the URL is similar to the one we are looking for.ExampleCode Implementation with implicit wait.import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import ... Read More
We can take screenshots of tests in Selenium webdriver. Capturing a screenshot is one of the most essential steps in failure analysis of failed tests. It is a three way step process to take screenshots.Firstly, we shall convert the webdriver object to the interface called the TakeScreenshot. Then we have to utilize the getScreenshotAs method to capture the image. The file format of the image to be captured is passed as parameter to that method.Finally, the file where the image is captured is to be copied to a location with the FileUtils.copyFile method.SyntaxFile s = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(s, new File("Image.png"));Exampleimport org.openqa.selenium.By; ... Read More
In this tutorial, we are going to write a program that finds the repeating element in the given array.Let's see the steps to solve the problem.Initialize the array.Initialize a counter map to store the frequency of each element in the array.Iterate over the array.Count each element.Print the element whose frequency is greater than 1.ExampleLet's see the code. Live Demo#include using namespace std; int findRepeatingElement(int arr[], int n) { map frequencies; for (int i = 0; i < n; i++) { map::iterator itr = frequencies.find(arr[i]); if (itr != frequencies.end()) { ... Read More
We can verify if an element does not exist in Selenium webdriver. To achieve this, we shall use the method getPageSource which gets the entire page source. So we can obtain a complete page source and check if the text of the element exists.We also use the findElements method and utilize any locators like xpath, CSS, and so on to identify the matching elements. The findElements gives an elements list.We shall count the number of elements returned by the list with the help of the size method. If the value of size is greater than 0, then the element exists ... Read More
In this tutorial, we are going to solve the following problem.Given a tank with a capacity of N liters and a pump that fill the tank with S speed per minute. Unfortunately, there is a hole in the tank. And water is wasting at a speed of WS per minute while filling it.We need to calculate the amount of water wasted for a full tank.The amount of water filled per minute is equal to the difference between the water filling water and wasting water speed.Hence we can get the total time to fill the water tank by dividing the capacity ... Read More
In this tutorial, we are going to write a program that finds all the pairs whose sum is equal to the given number in the binary search tree.We are going to store and values of trees in two different lists to find the pairs. Let's see the steps to solve the problem.Create a struct node for a binary tree.Write a function to insert a new node into the binary search tree.Remember, in the binary search tree all the elements that are less than root are smaller, and right are larger.Initialize two empty lists to store the left and right nodes ... Read More
We can integrate Sikuli scripts into Selenium webdriver. Sikuli is an automation tool which is open-source. It has the feature to capture the images on the elements as well as perform operations on them.Some of the advantages of Sikuli are −Desktop or Windows applications can be automated.Can be used for flash testing.Can be used on platforms like mobile, Mac, and Linux.It is based on image recognition technique.Can be easily integrated with Selenium.To integrate Sikuli with Selenium, follow the below steps − Navigate to the link − https://launchpad.net/sikuli/+download.Click on the jar to download it (which can be used for Java environments) ... Read More
We can select the dropdown option in Selenium webdriver. The dropdowns can be worked upon with the help of the Select class. The select tag is used to represent a dropdown and option tag is used to represent items in the dropdown in html.Let us investigate the html structure of a dropdown −We have to add the statement − import org.openqa.selenium.support.ui.Select to use the Select class methods. The Select class methods are listed below −selectByIndex(n) − An option is chosen based on the index of the option on the dropdown. The index n is passed as a parameter to the ... Read More