Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active July 7, 2023 17:27
Show Gist options
  • Save swapnilshrikhande/9819db0a1fc7c7ea4206d0807bd278ed to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/9819db0a1fc7c7ea4206d0807bd278ed to your computer and use it in GitHub Desktop.
Parent base class for all webdriver test. Logic to create instance of webdriver and clean up webdriver.
package com.eternussolutions;
import org.openqa.selenium.By;
import java.util.HashMap;
public class ElementData {
public String value;
public By selector;
public HashMap<String,By> selectors;
public String triggerEvent;
{
selectors = new HashMap<String,By>();
}
}
package com.eternussolutions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class PageObjectModel {
public static final String CLICK_EVENT = "click";
public static final String HOVER_EVENT = "hover";
// instance variables
WebDriver driver;
String baseURL;
public PageObjectModel(WebDriver webDriverP,String baseURLP){
this.driver = webDriverP;
this.baseURL = baseURLP;
}
//generic helper methods start
protected void clearInput(By selector){
driver.findElement(selector).clear();
}
protected void setValue(By selector, String value){
// does work of driver.findElement(By.id("ainput")).sendKeys("10");
driver.findElement(selector).sendKeys(value);
}
protected String getInputFieldValue(By selector) {
// does work of driver.findElement(By.id("coutput")).getAttribute("value")
String inputFieldValue = driver.findElement(selector).getAttribute("value");
return inputFieldValue;
}
protected Boolean isVisible(By selector){
//if(driver.)
return true;
}
protected void triggerEvent(By selector,String event) {
if(CLICK_EVENT.equalsIgnoreCase(event)){
driver.findElement(selector).click();
} else if(HOVER_EVENT.equalsIgnoreCase(event)){
}
}
//generic helper methods stop
}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="tests">
<!-- Test Suite Level Parameters -->
<parameter name="baseUrl" value="http://swapnilshrikhande.github.io/" />
<test name="FireFoxTest">
<!-- Test Level Parameters -->
<parameter name="browser" value="firefox" />
<classes>
<class name="com.eternussolutions.AdditionTest"/>
</classes>
</test>
<test name="ChromeTest">
<!-- Test Level Parameters -->
<parameter name="browser" value="chrome" />
<classes>
<class name="com.eternussolutions.AdditionTest"/>
</classes>
</test>
<test name="IETest">
<!-- Test Level Parameters -->
<parameter name="browser" value="ie" />
<classes>
<class name="com.eternussolutions.AdditionTest"/>
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
package com.eternussolutions;
import static org.testng.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
public class WebDriverTest {
protected boolean acceptNextAlert = true;
protected StringBuffer verificationErrors = new StringBuffer();
protected WebDriver driver;
protected String baseUrl;
@Parameters({"browser","baseUrl"})
@BeforeClass(alwaysRun = true)
public void setUp(String browser,String baseUrlProperty) throws Exception {
initialize(browser,baseUrlProperty);
}
protected WebDriver getWebdriver(String browser){
WebDriver driver = null;
// If the browser is Firefox, then do this
if(browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "drivers\\geckodriver.exe");
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "drivers\\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
driver = new InternetExplorerDriver(capabilities);
}
return driver;
}
protected void initialize(String browser,String baseUrlProperty){
baseUrl = baseUrlProperty;
driver = getWebdriver(browser);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
cleanUp();
}
protected void cleanUp(){
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
//Common Utility Methods
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment