• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Selenium

WebDriver Code Snippets

Overview

In this tutorial, we will learn some of the useful Webdriver code snippets useful across test automation programs.

How to Accept Alert?

public void AcceptAlert() throws Throwable {
 boolean status = false;
 Alert alert = null;

 try {
 alert = driver.switchTo().alert();
 alert.accept();
 status = true;
 } catch (NoAlertPresentException ex) {
 
 ex.printStackTrace();
 } finally {
 if (status) {
 System.out.println("Alert Success "); 
 } else{
 System.out.println("Alert Failure : no alert to handle");
 }
 }

 }

How to wait for visibility of Element on a web page ?

public boolean waitForVisibilityOfElement(By by)
 throws Throwable {
 boolean status = false;
 wait = new WebDriverWait(driver, 45);
 try {
 wait.until(ExpectedConditions.
visibilityOfElementLocated(by));
 status = true;
 return status;
 } catch (Exception e) {

 return status;
 } finally {
 if (status ) {
 System.out.println("wait for visibility success "); 
 } else {
 System.out.println("wait for visibility failed");
 }
 }
 }

How to wait for invisibility of Element on a web page ?

The code snippet is almost same as above except for a small change.

public boolean waitForInVisibilityOfElement(By by)
 throws Throwable {
 boolean status = false;
 wait = new WebDriverWait(driver, 45);
 try {
 wait.until(ExpectedConditions.
invisibilityOfElementLocated(by));
 status = true;
 return status;
 } catch (Exception e) {
 return status;
 } finally {
 if (status) {
 System.out.println("wait for invisibility success... ");
 } else {
 System.out.println("wait for invisibility failure... ");
 }
 }

 }

How to get HTML table row count ?

public int getTableRowCount(By locator) throws Exception {

 WebElement table = driver.findElement(locator);
 List<WebElement> rows = table.findElements(By.tagName("tr"));
 int rowcount = rows.size() - 1;
 return rowcount ;
 }

 

How to get HTML table column count?

public int getTableColumncount(By locator) throws Exception {

 WebElement tr = driver.findElement(locator);
 List<WebElement> columns = tr.
findElements(By.tagName("td"));
 int colcount = columns.size() ;
 return colcount;

 }

 

Selenium Tutorial on this website:

https://www.testingdocs.com/selenium-webdriver-tutorial/

More information on Selenium, visit official website:

https://www.selenium.dev/

Related Posts

Windows 10 Settings

Selenium /

Add Microsoft Webdriver on Windows OS

Download Selenium Components

Selenium /

Getting Started with Selenium Webdriver

LambdaTest Testing Cloud SaaS Platform

Selenium /

LambdaTest – Testing Cloud SaaS Platform

Selenium /

Selenium 3.0 and Mozilla GeckoDriver

SauceLabs Website

Selenium /

Run an Example Test on SauceLabs

‹ Difference between Implicit and Explicit Wait› Run BrowserStack Cloud Test

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com