Different Techniques Of Debugging Selenium Based Test Scripts

What is Debugging?

  1. Identification: The first step of debugging is to discover the issue and try reproducing the same in the local system to know why the issue occurred. This is an important step as we need to identify the root cause of the issue to deploy a permanent solution.
  2. Isolation: This is the second step where the idea is to separate the buggy code from the healthier code. The unit testing of buggy code is required to identify the steps that need to be performed to fix the issue. Isolation of buggy code would further reduce time and not affect the other code.
  3. Resolution: This is the primary step toward fixing the buggy code. This stage is dependent on the above two mentioned stages, the resolution can be deployed as soon as the above two stages are completed. Few tips for fixing the code:
  • Deep dive into the code and libraries being used to get an understanding of the working of the framework
  • Refer to the proper document and solutions on StackOverflow
  • Execute the code in debug mode
  • Perform code walkthrough and unit testing in the local system
  • Refactor or re-design the framework architecture in the worst-case scenario
  1. Review: This is the final stage of debugging that developers usually try to skip. Reviewing is done to ensure that the fix deployed is working fine and not hampering the other code. Ideally, the review should be done by both i.e the developer who actually fixed the buggy code and by the other developer who is responsible for reviewing and giving a green signal for code merge.

Various Techniques Of Debugging Test Automation Scripts

  • Loggers: It contains all the information about the logging level. Loggers offer different severity levels for logs. The initial step is to create an object of the Logger class:
  • All
  • Debug
  • Info
  • Warn
  • Error
  • Fatal
  • Off
  • Appenders: The logs that have been generated with the above-mentioned types of severity log levels have to be pushed to the destination to view the logs, this role is performed by appenders. It sends the log events to the destination folder or prints on the console as per the configuration done. There are three types of appenders available to output the logs:
  • ConsoleAppender
  • FileAppender
  • Rolling File Appender
  • Layouts: Layouts provide different methods for the formatting of the logs. The logging information can be formatted in different formats with the below-provided methods:
  • Public static Logger getRootLogger()
  • Public static Logger getLogger (String name)
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;public class TestScript { private WebDriver driver;
public String expectedTitle = "This is a wrong title";
@BeforeClass
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
@Test
public void verifyLoginPage() {
driver.get("https://www.pcloudy.com/");
WebElement loginButton =
driver.findElement(By.xpath("//a[text()='Login']"));
loginButton.click();
String actualTitle = driver.getTitle(); Assert.assertEquals(actualTitle, expectedTitle,
"Login Page Title didn't matched with the expected title");
}
@AfterMethod
public void screenShot(ITestResult result) {
if (ITestResult.FAILURE == result.getStatus()) {
try {
TakesScreenshot screenshot =
(TakesScreenshot) driver;
File src =
screenshot.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new
File("/home/ramit/Pictures/" + result.getName() + ".png"));
System.out.println("Screenshot captured of
failed test case");
} catch (Exception e) {
System.out.println("Exception
occured while taking screenshot " + e.getMessage());
}
}
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
  • Set up the breakpoints where buggy code is observed
  • Execute the source code in debug mode
  • Validate the data returned in the debugger
  • Resume the debugging if multiple breakpoints are added
  • Stop the debug mode execution
  • Fix error and deploy the code
  • For the remote debugging ports, you can specify any open port.
  • For the user data directory, you need to specify the directory where the new chrome profile will be created.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;public class TestScript {

private WebDriver driver;
public String expectedTitle = "Remote Mobile Web &
Application Testing on Real Android Devices - pCloudy";

@BeforeClass
public void setup()
{
WebDriverManager.chromedriver().setup();
ChromeOptions opt = new ChromeOptions();
opt.setExperimentalOption("debuggerAddress",
"localhost:9222");
driver = new ChromeDriver(opt);
}
@Test
public void verifyLoginPage()
{
//driver.get("https://www.pcloudy.com/");

WebElement loginButton =
driver.findElement(By.xpath("//a[text()='Login']"));
loginButton.click();

driver.findElement(By.id("userId")).sendKeys("ramit.dhamija@gmail.com");

String actualTitle = driver.getTitle();

Assert.assertEquals(actualTitle,
expectedTitle,"Login Page Title didn't matched with the expected
title");
}


@AfterClass
public void tearDown()
{
driver.quit();
}
}

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
pCloudy

pCloudy

pCloudy is the most powerful cloud-based App Testing Platform. Brand Marketing @ pCloudy (www.pcloudy.com)