Created
December 28, 2018 17:36
-
-
Save pragmatictesters/24aedaa01a154d85d6803857e730930d to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Sample TestBase class with support for cross browser testing with headless capabilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pragmatic.selenium.hrm; | |
import com.github.javafaker.Faker; | |
import com.pragmatic.selenium.hrm.pages.LoginPage; | |
import io.github.bonigarcia.wdm.WebDriverManager; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebDriverException; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.chrome.ChromeOptions; | |
import org.openqa.selenium.edge.EdgeDriver; | |
import org.openqa.selenium.firefox.FirefoxBinary; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.firefox.FirefoxOptions; | |
import org.openqa.selenium.ie.InternetExplorerDriver; | |
import org.openqa.selenium.opera.OperaDriver; | |
import org.openqa.selenium.opera.OperaOptions; | |
import org.openqa.selenium.safari.SafariDriver; | |
import org.openqa.selenium.support.PageFactory; | |
import org.testng.annotations.*; | |
import java.util.concurrent.TimeUnit; | |
public class TestBase { | |
public static Faker faker; | |
public static String browser; | |
public WebDriver driver; | |
public static void navaigateToLoginPage(WebDriver driver) { | |
driver.get(Constants.BASE_URL); | |
} | |
public static void navaigateToLoginPage(WebDriver driver, String URL) { | |
driver.get(URL); | |
} | |
public static void loginToSystem(WebDriver driver) { | |
navaigateToLoginPage(driver); | |
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class); | |
loginPage.typeUsername(Constants.ADMIN_USERNAME).typePassword(Constants.ADMIN_PASSWORD).clickLoginSuccess(driver); | |
} | |
@Parameters("browser") | |
@BeforeSuite(groups = {"smoke", "regression"}) | |
public void beforeSuite(@Optional("chrome") String browser) { | |
initWebDrivers(); | |
faker = new Faker(); | |
TestBase.browser = browser; | |
} | |
public void initWebDrivers() { | |
WebDriverManager.chromedriver().setup(); | |
WebDriverManager.firefoxdriver().setup(); | |
WebDriverManager.operadriver().setup(); | |
WebDriverManager.iedriver().setup(); | |
WebDriverManager.edgedriver().setup(); | |
} | |
@BeforeMethod(groups = {"smoke", "regression"}) | |
public void beforeMethod() { | |
launchWebBrowser(); | |
setImplicitWait(); | |
maximizeBrowserWindow(); | |
loginToSystem(driver); | |
} | |
public void maximizeBrowserWindow() { | |
//driver.manage().window().maximize(); | |
driver.manage().window().fullscreen(); | |
} | |
public void setImplicitWait() { | |
driver.manage().timeouts().implicitlyWait(Constants.TIMEOUT_IMPLICIT, TimeUnit.SECONDS); | |
} | |
public void launchWebBrowser() { | |
try { | |
if (browser.equalsIgnoreCase("chrome")) { | |
ChromeOptions options = new ChromeOptions(); | |
options.addArguments("disable-infobars"); | |
driver = new ChromeDriver(options); | |
} else if (browser.equalsIgnoreCase("firefox")) { | |
driver = new FirefoxDriver(); | |
} else if (browser.equalsIgnoreCase("safari")) { | |
driver = new SafariDriver(); | |
} else if (browser.equalsIgnoreCase("opera")) { | |
OperaOptions options = new OperaOptions(); | |
options.setBinary(""); | |
driver = new OperaDriver(options); | |
} else if (browser.equalsIgnoreCase("ie")) { | |
driver = new InternetExplorerDriver(); | |
} else if (browser.equalsIgnoreCase("edge")) { | |
driver = new EdgeDriver(); | |
} else if (browser.equalsIgnoreCase("chrome-headless")) { | |
ChromeOptions options = new ChromeOptions(); | |
options.addArguments("headless"); | |
driver = new ChromeDriver(options); | |
} else if (browser.equalsIgnoreCase("firefox-headless")) { | |
FirefoxBinary firefoxBinary = new FirefoxBinary(); | |
firefoxBinary.addCommandLineOptions("--headless"); | |
FirefoxOptions firefoxOptions = new FirefoxOptions(); | |
firefoxOptions.setBinary(firefoxBinary); | |
driver = new FirefoxDriver(firefoxOptions); | |
} else { | |
try { | |
throw new BrowserLaunchException(browser); | |
} catch (BrowserLaunchException e) { | |
e.printStackTrace(); | |
System.exit(-1); | |
} | |
} | |
} catch (WebDriverException e) { | |
e.printStackTrace(); | |
System.exit(-1); | |
} | |
} | |
@AfterMethod(groups = {"smoke", "regression"}) | |
public void afterMethod() { | |
driver.quit(); | |
} | |
public void check(WebElement chkCreateLoginDetails) { | |
if (!chkCreateLoginDetails.isSelected()) { | |
chkCreateLoginDetails.click(); | |
} | |
} | |
public void uncheck(WebElement chkCreateLoginDetails) { | |
if (chkCreateLoginDetails.isSelected()) { | |
chkCreateLoginDetails.click(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment