Skip to main content

Posts

Showing posts with the label Selenium

Download Restrictions option in Selenium

At times, we need to visit a page in our application that automatically starts downloading the file which is as per the feature of that page. But we're navigating to this page (A) only to further navigate to the other page (B) by clicking on some link given on this page (A) which redirect us to the page (B) but at the same time, we don't want to download the file on visiting this page (A) as it's not required every time while running our Selenium automation script. Did you know there is a preference option "download_restrictions" that can help us achieve this? Let say A is this: http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip HashMap<String, Object> prefs = new HashMap<String, Object>(); prefs.put("download_restrictions", 3); ChromeOptions opt = new ChromeOptions(); opt.setExperimentalOption("prefs", prefs); WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(opt);

What happens when you also need to test how your application behaves on a slow connection using Selenium?

Execute Selenium test suite on a slow network connection : public class SetNetworkConditions { public static WebDriver driver; @Test public static void setSlowNetwork () throws IOException { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); CommandExecutor executor = ((RemoteWebDriver) driver).getCommandExecutor(); // Setting the slow network conditions Map<String, Comparable> map = new HashMap<String, Comparable>(); map.put(“offline”, false); map.put(“latency”, 5); map.put(“download_throughput”, 5000); map.put(“upload_throughput”, 5000); Response response = executor.execute(new Command(((RemoteWebDriver) driver).getSessionId(), “setNetworkConditions”, ImmutableMap.of(“network_conditions”, ImmutableMap.copyOf(map)))); driver.get(“https://testersdigest.blogspot.com"); long navigationStart = (long) ((JavascriptExecutor) driver) .executeScript(“return window.performa

Copy Data From One Excel To Another Using Apache POI

In case there is a requirement where you need to copy the data from one excel file into another excel using test automation, we can use Apache POI open-source library to do that. Here's the self-explanatory code but do let me know if you have any queries. For other details, you can go through https://www.javatpoint.com/apache-poi-tutorial import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.testng.annotations.Test; public class CopyExcel { @Test public static void CopyOneWBookToOther() throws IOException { // Step #1 : Locate path and file name of target and output excel. String TargetSheetPathAndName = "/Users/dheerajgambhir/Desktop/O

A road map to learn Web Automation (SDET)

Good developers are rare, good SDETs are rare as well and always high in demand. Without a doubt, Automation testing has become a need of the hour. You need to take baby steps as you plan to start automation testing from scratch. I wrote this article - "A Road map that will help you learn Web Automation", see if this helps: https://www.linkedin.com/posts/dheerajgambhir_automation-selenium-git-activity-6568387687412273152-SjE8/ Always remember, Test automation is a development discipline.

Salesforce login OTP feature automation

Of course, there are many ways to tackle "the Salesforce login OTP feature" for automation but the easiest one is to change the Trusted IP Range i.e. Add the IP address of the server where you are executing your script to trusted IP range in SF. Under Setup menu, go to Security Controls - Network Access and here you need to enter the IP address ranges of your machine/server as shown below. After lightning upgrade, I agree that execution against dynamic content and Shadow DOM still poses some challenges that usually end up consuming a lot of time than any other usual web application but with correct XPATH strategy it can be solved.

Real-time test execution metrics (time-series)

We run most of the automated suites in a remote machine and thus the test results are accessible only at the end of the execution OR else, we need to check the console for the results for the progress. What if we can have a way that gives us the execution results while the tests are actually being executed in the remote machines? Yes, it's possible by using time-series database like InfluxDB, Graphite, etc. that supports insertion and real-time querying of data via a SQL-like query language, so we can use it to collect all the test metrics and then use Grafana or Kibana which is an excellent and powerful visualization tool to form a dashboard. How to install InFluxDB and Grafana on windows: Please go through these well elaborative articles by Antoine Solnichkin: https://devconnected.com/how-to-install-influxdb-on-windows-in-2019/ https://devconnected.com/how-to-install-grafana-on-windows-8-10/ For Sample queries, please go through these links: https://docs.i

Soft Assertions: Where and when we can use it?

To tackle the disadvantage of Hard Assertions where we want to continue the execution even if some assert fails and see the result at the end of the test. Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and continue with the next step after assert statement. This is in the case where our test requires multiple assertions to be executed and we want all of the assertions to be executed first before marking (failing/skipping) the tests. Consider these 2 examples: 1) We're verifying many CSS values of the same element like background-color, font-family, and color in a single test case. element.getAttribute("background-color") element.getCssValue("font-family") element.getCssValue("color") Obviously, if any one of these fails we would like to catch it and mark the test case as fail but that doesn't mean if background-color verification fails it should mark the test case fail with

The Base class in the Selenium framework

Authoring automated tests is quite easy but authoring easily human-readable and maintainable tests is much tougher. Especially, when your project grows in size and complexity. There are many approaches that can help you build your tests in a Selenium framework like correct handling of your Base Class well. The Base class should have these things: - the setup() and teardown() test methods. - the WebDriver object. - load your configuration/properties file here (can be a part of its constructor) - common aspects of all tests like handling sync issues, ScreenshotOnFailure, etc. - have a method to visit the site that returns the Index page object. - and All test classes should inherit it. P.S. My view on this topic is purely subjective. And in many cases, you may not be able to apply this type of approach to your framework.

3 Selenium classes that are not so popular to locate elements

Here we have discussed 3 Selenium classes that are not so popular but that can help you locate elements in Selenium: - ByIdOrName - ByChained - ByAll 1) ByIdOrName - This helps the driver to locate an element either by name or by id. This is present under org.openqa.selenium.support package and to use this we have to call the constructor of the ByIdOrName class and pass the Name or ID. This method tries to find the element using ID first and it waits till the max implicit wait time for the element to locate using ID and if it is not able to find the element with ID then only it tries to locate with the Name. As soon as the driver finds the element with id, it will not check the element with a name attribute and won't want for max implicit wait time either. driver.findElement(new ByIdOrName("username")).click(); 2) ByChained - This helps the driver to locate the element based on the parent element, and it accepts an unlimited number of locators. This is prese

Installing and Uninstalling Add-ons in Firefox Browser

Almost a month back, we discussed how we can install an extension in Selenium 3: https://www.linkedin.com/posts/dheerajgambhir_seleniumautomation-chromeext-automationtesting-activity-6587948591678095361-Lr7m Or https://testersdigest.blogspot.com/2019/10/need-extension-while-running-your-se.html And now in Selenium 4, we have direct method installExtension and uninstallExtension for the firefox Driver. The installExtension installs a new addon with the current session which inturn will return an ID that may later be used to uninstallExtension the addon using uninstallAddon.

@FindBy Limitation to accept only Static String

Completely agree with Simon, we should get something better here. @FindBy annotations accept String constant values. You can't compute them at runtime and always have to go with mix and match approach here like PageFactory annotations for static ones and By's in for dynamically created element(s). There are few workarounds that we’ve tried but nothing solid yet. hashtag # FindBy hashtag # testautomation hashtag # Selenium

Zoom In And Zoom Out In Selenium using JavascriptExecutor

Zoom In And Zoom Out In Selenium using JavascriptExecutor: public void zoomInOut(String value) { JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.body.style.zoom='" + value + "'"); } And use the above function as per your need like: public void validateZoom() { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://testersdigest.blogspot.com"); zoomInOut("150%"); zoomInOut("10%"); zoomInOut("40%"); }

How to make sure that your Selenium automation test works fine even for slow loading web pages?

Rather than using methods like waitForPageToComplete() or document.readyState property etc. to wait for a page to load in your Se tests, the better way is to use the LoadableComponent and SlowLoadableComponent pattern in Page Object Model. You need to make sure that your Page Object classes extends the abstract LoadableComponent class which in turn provides the implementation for the following methods: - load() //The load method is used to navigate to the page - isLoaded() throws java.lang.Error //The isLoaded method is used to determine whether we are on the right page. The get() method from the LoadableComponent class will ensure that the page/component is loaded by invoking the isLoaded() method. If the check fails here, load() method is called followed by isLoaded(). If your site loads very slow, then we should consider using SlowLoadableComponent instead of LoadableComponent. SlowLoadableComponent verifies the page is loaded until the given maximum timeout.

Selenium Event Listener

Selenium provides an API for various events that occur when our scripts are executed like click, switch to a window, navigation, onException, etc. and these can be registered using WebDriverEventListener interface or EventFiringWebDriver class. These events play a pivotal role in analyzing results and in debugging issues (if any). EventFiringWebDriver class takes care of all events that are occurred while the execution of your script. EventFiringWebDriver class can be bind to more than one listener but all event listeners should be registered with the EventFiringWebDriver class so that it can be notified. EventFiringWebDriver eventFiring =new EventFiringWebDriver(driver); FirstEventCapture firstListener =new FirstEventCapture(); //FirstEventCapture implements WebDriverEventListener SecondEventCapture secondListener =new SecondEventCapture(); //SecondEventCapture implements WebDriverEventListener eventFiring.register(firstListener); eventFiring.register(secondListener); Check t

Measuring page load time using Selenium

There are many tools that are designed specifically to measure a load of your web pages such as LoadRunner, JMeter, WebLoad, etc. and preference should be given to these tools only. But in case, there is an ask to do it using Selenium, we can use Performance Timing and Navigation Interface APIs to measure the page load time on client-side: - https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming - https://www.w3.org/TR/navigation-timing/#sec-navigation-timing-interface The first image shows when we are executing it manually on the Chrome Dev Tools console. We can use the output generated by these APIs in our Se framework (if required) as shown in the Second Image. P.S. As from Se4 alpha-3 onwards, we can directly play with Dev Tools- I will check if we can directly use it instead of capturing time using these APIs.  #selenium   #loadTime   #devTools   #testAutomation   #seleniumautomation