LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How do I execute UI tests in cloud?

Can anyone please give me a tutorial on how do I execute UI tests in cloud.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


User Interface testing or UI testing is the type of testing whereby we check if the User Interface for the web application works fine or has any defect which hampers user behaviour and does not meet the written specification.

Before moving onto executing tests on the cloud using a Selenium Grid, it’s important that we know how we can automate our tests to utilise the maximum benefit of the cloud.

Like any other type of testing, UI testing can also be performed either manually or through automation.

  • Selenium test Automation for UI testing requires us to write the tests only once and run them again and again. This is done without any intervention with different values and different scenarios, ultimately increasing our testing speed.

  • Automating UI tests for UI testing helps us in executing tests with an efficient manner, with the probability of errors going down.

  • Automating UI tests also helps in building reports quickly and sharing with the team as soon as the testing is completed bringing in more transparency in the testing process.

Let’s try running our a test case on LambdaTest Cloud Selenium Grid. You can use LambdaTest Desired Capabilities Generator for configuring the desired capabilities object. This would save you a lot of your time spent in Selenium Grid setup when done manually.

With LambdaTest, you only need to create a Remote Server, add your LambdaTest username, access key, and the Grid URL in your script, and then you are good to go.

Below is the Java code in the TestNG framework that would be more beneficial in this scenario:

package lambdatest;
import http://java.net.MalformedURLException;
import http://java.net.URL;
import http://org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SampleTest {
  public String username = "enteryourusernamehere";
 public String accesskey = "enteryourkeyhere";
 public RemoteWebDriver driver = null;
 public String gridURL = "@http://hub.lambdatest.com/wd/hub";
 boolean status = false;
 @BeforeTest
 @org.testng.annotations.Parameters(value={"browser","version","platform"})
 public void setUp(String browser, String version, String platform) throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setCapability("browserName", browser);
 capabilities.setCapability("version", version);
 capabilities.setCapability("platform", platform); // If this cap isn't specified, it will just get the any available one
 capabilities.setCapability("build", "Selenium Grid");
 capabilities.setCapability("name", "Sample Test");
 capabilities.setCapability("network", true); // To enable network logs
 capabilities.setCapability("visual", true); // To enable step by step screenshot
 capabilities.setCapability("video", true); // To enable video recording
 capabilities.setCapability("console", true); // To capture console logs
 try {
 driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
 } catch (MalformedURLException e) {
 System.out.println("Invalid grid URL");
 } catch (Exception e) {
 System.out.println(e.getMessage());
 }
 }
 @Test
 public void testSimple() throws Exception {
  try {
 driver.get("Apple");
 driver.manage().window().maximize();
 driver.findElement(By.xpath("//*[@id=\'ac-globalnav\']/div/ul[2]/li[3]")).click();
 Thread.sleep(2000);
 driver.findElement(
 By.cssSelector("#chapternav > div > ul > li.chapternav-item.chapternav-item-ipad-air > a")).click();
 Thread.sleep(2000);
 driver.findElement(By.linkText("Why iPad")).click();
 Thread.sleep(2000);
 } catch (Exception e) {
 System.out.println(e.getMessage());
 }
 }
 @AfterTest
 public void tearDown() throws Exception {
  if (driver != null) {
 ((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
 driver.quit();
 }
 }
}

Now, we would run this Java class file with the XML file that contains the value of parameters passed in the Java file and also helps in creating a suite of different classes, using which, we can perform automation testing with Selenium Grid for parallel execution on different configurations.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="BlogSuite" parallel="tests">
 <test name="FirefoxTest">
 <parameter name="browser" value="firefox"/>
 <parameter name="version" value="62.0"/>
 <parameter name="platform" value="WIN8"/>
 <classes>
 <class name="lambdatest.SampleTest"/>
 </classes>
 </test> 
 <test name="ChromeTest">
 <parameter name="browser" value="chrome"/>
 <parameter name="version" value="70.0"/>
 <parameter name="platform" value="WIN10"/>
 <classes>
 <class name="lambdatest.SampleTest"/>
 </classes>
 </test> 
 <test name="SafariTest">
 <parameter name="browser" value="safari"/>
 <parameter name="version" value="11.0"/>
 <parameter name="platform" value="macos High Sierra"/>
 <classes>
 <class name="lambdatest.SampleTest"/>
 </classes>
 </test> 
</suite>

Output Screen:

Now, we have successfully executed automation testing with Selenium Grid for parallel execution on different browsers along with different operating systems without any hassle of creating a hub and launching the nodes on different ports. While the test is running on a Selenium Grid, you can see the live video streaming of your tests and various other details such as commands which includes the screenshot of each command passed by your script and also the logs and exceptions raised.

Up Vote Down Vote 0 Votes
Posted 3 months ago