LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

What are the top cross-browser testing trends in 2020?

Can anyone please tell me about some of the top cross-browser testing trends in 2020.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


Multi Browser Testing or Cross Browser Testing

Under most circumstances, multi-browser testing and cross browser testing refer to same thing. Multi browser testing, as the name itself suggests, is the type of testing in which your site is tested over a combination of multiple browsers to ensure that it is working as expected across all browsers and providing same user experience to your audience.

Using multiple libraries for different UI implementations may decrease the time taken to develop the website. Due to these UI implementations, it is hard to ensure that your website will perform well across all browsers as it is performing over one browser.

But it takes a considerable amount of time to test your site on every browser manually, so we have leveraged the power of automation with Selenium and TestNG framework to reduce the testing efforts and get desired results in the minimum time possible. TestNG gives us the freedom to perform similar tests across different browsers in a straightforward manner.

Executing Multi Browser Testing

Here, we will be using a combination of Selenium with TestNG while using the @Parameter annotation to parameterize the test script with different standards of browser. Although you can also use Selenium Grid for multi browser testing of your site, we will be performing multi browser testing with TestNG and Selenium WebDriver.

As we are using TestNG with Selenium WebDriver, we can automate our test cases with the help of Internet Explorer, Chrome, Safari, or Mozilla browsers. Also, with the help of @Parameter annotation, we will be able to pass multiple values of browsers via test script from the testing.xml file. Then the browser parameter value can be used to represent the corresponding driver class of Selenium WebDriver. Also, as the browser value will be implemented across all the test methods, so it ideal to utilize the browser variable in @BeforeTest method.

Thus, our test script with TestNG @Parameter annotation and @BeforeTest method will look something like this:

Package Automation Frame Work;

import http://org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import http://org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MultiBrowser {
public WebDriver driver;
@Parameters(“browser”)
@BeforeClass
// Passing Browser parameter from TestNG xml
Public void beforeTest(String browser) {
// If the browser is Firefox, then do this
if(browser.equalsIgnoreCase(“firefox”)) {
driver = new FirefoxDriver();
// If the browser is IE, then do this 
}else if (browser.equalsIgnoreCase(“ie”)) {
// Here I am setting up the path for my IEDriver
System.setProperty(“http://webdriver.ie.driver”, “D:\ToolsQA\OnlineStore\drivers\IEDriverServer.exe”);
driver = new InternetExplorerDriver();
} 
// Doesn’t the browser type, lauch the Website
driver.get(“http://www.store.demoqa.com”); 
}
// Once Before method is completed, Test method will start
@Test public void login() throws InterruptedException {
driver.findElement(By.xpath(“.//*[@id=’account’]/a”)).click();
driver.findElement(http://By.id(“log”)).sendKeys(“testuser_1”);
driver.findElement(http://By.id(“pwd”)).sendKeys(“Test@123”);
driver.findElement(http://By.id(“login”)).click();
}
@AfterClass public void afterTest() {
driver.quit();
}
}

Now, you would have to create a testing.xml file to run your test, and configure this XML file to pass the parameters, which in turn means to define the browsers that you want to use for running the test.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="automationFramework.MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="automationFramework.MultiBrowser" />
</classes>
</test>
</suite>

Note: You can set multiple browsers in the testing.xml file, we have used two main browsers just for example purposes.

After configuring the testing.xml file, run the test by right-clicking on the XML file and select Run as TestNG Suite.

And that’s it!

TestNG will start executing tests one by one for the browsers that you have enlisted in your script. However, there is a better alternative for automated multi browser testing of your web app – LambdaTest, an online Selenium Grid that makes it easier for you to test cross browser compatibility of your site.

Now, you might be curious to know about parallel testing of your site, so let’s move to the next topic.

Running Parallel Tests with TestNG

To understand how to run test scripts in parallel, first, it is crucial to understand session handling. While executing test cases, Selenium WebDriver has to interact with the enlisted browsers to execute the given commands. Thus, while running the tests, there are chances that someone else starts executing another script in the same machine and in the same browser type before the current execution completes.

When a situation like this occurs, you need a mechanism that stops these two executions from overlapping each other. And this is only achievable via session handling in Selenium.

But, how can you achieve session handling in Selenium?

The source code of Selenium WebDriver contains a variable named “sessionId.” So, whenever a new instance of a WebDriver object is created, a new “sessionId” will also be generated, attached with the specific browser driver. For example, a new session for Firefox browser will generate in:

webdriver=new FirefoxDriver ();

Thus, whatever you do after this will execute only in that particular Firefox browser session. This is an in-built functionality, so it doesn’t require you to assign the sessionId explicitly.

Executing Parallel Testing for Your Site

There are many scenarios in web app testing when users need to execute multiple tests in same or different browsers at the same time. This type of testing refers to parallel testing of web app.

TestNG provides numerous ways to run tests in parallel. The simplest way is to use “parallel” attribute in the testing.xml file. There are four different values that the parallel attribute can accept.

tests – To run all the test cases in parallel inside the “test” tag of XML file

classes – To run all the test cases in parallel in a java class

methods – To execute all the methods in parallel with @Test annotation

instances – To execute test cases parallely in the same instance, but two methods of two different instances will run in different thread

If you set the ‘parallel’ attribute on the tag to ‘tests’ in the XML file, TestNG will run all the @Test tags in a different thread. To better understand the parallel testing, let’s take the example of the above code that we used for multi browser testing with two different browsers.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="automationFramework.MultiBrowser" />
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="automationFramework.MultiBrowser" />
</classes>
</test>
</suite>

In the above testing.xml file, we will set the ‘parallel’ attribute to ‘tests,’ and then run it. On running the XML file, you will see that both the browsers will open concurrently, and your tests will run in parallel.

The above process is great if you want to test on the latest 2-3 most popular browsers that you may install locally. However, if you wish to run tests on a wider range of browsers combinations, which you actually should, then you can use online selenium grid providers like LambdaTest Selenium Grid to perform parallel testing of your website.

Up Vote Down Vote 0 Votes
Posted 3 months ago