Wednesday, March 9, 2022

Selenium WebDriver Architecture

 It consists of Selenium IDE, Selenium RC, Selenium WebDriver and Selenium Grid.

Selenium IDE:
Selenium IDE (Integrated Development Environment) is a Firefox & Chrome plugin. It allows us to record and playback the scripts.

Selenium RC:
It is basically a HTTP Proxy Server which injects JavaScript into a browser to automate the actions.

Selenium WebDriver:
Selenium WebDriver is a browser automation framework that accepts commands and sends them to a browser. It is implemented through a browser-specific driver. It controls the browser by directly communicating with it.
Selenium WebDriver supports Java, C#, PHP, Python, Perl, Ruby. Operation System Windows, Mac OS, Linux, Solaris. Browsers Mozilla Firefox, Internet Explorer, Microsoft Edge, Google Chrome, Safari, Opera Android, iOS, HtmlUnit.

Selenium Grid:
It is a Selenium API which can be used to run automation tests on multiple browsers and operating systems.

The Selenium WebDriver Architecture follows the popular Client-Server architecture and consists mainly of four components:

  • Selenium Client and Language Bindings
  • JSON Wire Protocol over HTTP
  • Browser Drivers
  • Web Browsers


How to capture browser logs for failed tests

 

public static void getBrowserLog(WebDriver driver) {
        Logs log = driver.manage().logs();
        LogEntries logEntries = log.get(LogType.BROWSER);
        APP_LOGS.info("======== Browser log - starts ========");
        for (LogEntry logEntry : logEntries) {
            APP_LOGS.info(logEntry.getMessage() + "\n");
        }
        APP_LOGS.info("======== Browser log - ends ========");
    } 


Note: Call the above method in onTestFailure method of TestNG listener.

Wednesday, February 23, 2022

How to Automate Slider in Selenium WebDriver

We can achieve it using xOffse and yOffse of the web element and drag and drop by method.

WebElement slider = driver.findElement(By.xpath("//*[@id='slider']/a"));
Actions action = new Actions(driver);
action.dragAndDropBy(source, xOffset, yOffset).build.perform();
action.click();
action.release();

Note: We can get the X,Y coordinates using Page Ruler Redux chrome plugin.
xOffset horizontal move offset (Top value in the Page Ruler).
yOffset vertical move offset (Right value in the Page Ruler).

Monday, January 10, 2022

Taking screenshots for soft asserts

 

it is advisable to take screenshots of failed test cases for further analysis and proof of failure.

 public class CustomSoftAssert extends SoftAssert {
 

private static final String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";


    @Override
    public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {
        try {
            WebDriver driver = TestBase.getDriver();
         
            // Take screenshot
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh.mm.ss.S");
            String currentTimeInstance = formatter.format(date);
            String screenshotPath = System.getProperty("user.dir")+"/Screenshots"+ "-(" + currentTimeInstance
                    + ").jpg";
         
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

            FileUtils.copyFile(scrFile, new File(screenshotPath));


            // To add screenshot to ReportNG HTML report as a link
            System.setProperty(ESCAPE_PROPERTY, "false");
            Reporter.log("<a href=\"" + screenshotPath + "\">Screenshot</a>");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}