Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package base;
- import models.ui.Config;
- import models.ui.ExportTestData;
- import models.ui.TestData;
- import org.apache.commons.lang3.StringUtils;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.openqa.selenium.remote.DesiredCapabilities;
- import org.openqa.selenium.remote.RemoteWebDriver;
- import org.openqa.selenium.support.PageFactory;
- import org.testng.annotations.AfterMethod;
- import org.testng.annotations.AfterSuite;
- import org.testng.annotations.BeforeMethod;
- import org.testng.annotations.BeforeSuite;
- import pages.WhatfixLandingPage;
- import pages.WhatfixLoginPage;
- import util.YamlReader;
- import java.io.File;
- import java.lang.reflect.Method;
- import java.net.MalformedURLException;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by chandanjavaregowda on 03/04/17.
- */
- public abstract class BaseTest {
- protected static TestData data;
- protected static ExportTestData exportData;
- private static Config config;
- private static DesiredCapabilities capabilities;
- public static WebDriver driver;
- private String jenkinsUrl;
- private String url;
- private String path;
- private boolean isLoginLogoutRequired = false;
- protected int flowsBeforeAddition = 0;
- private String username;
- private String password;
- // set up config and data
- static {
- config = new YamlReader("config.yml").readConfig();
- SetIpAddress ipAddress = new SetIpAddress();
- data = ipAddress.setData();
- exportData = new YamlReader("exportData.yml").readExportTestData();
- }
- @BeforeSuite
- public void setUp() {
- try {
- setBrowser();
- if (config.isEnableWhatfixPlugin()) {
- enableWhatfixExtension();
- }
- driver = new RemoteWebDriver(new URL(config.getNodeUrl()), capabilities);
- if (config.isBringToFront()) {
- bringToForeground();
- }
- if (config.isMaximize()) {
- maximize();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- }
- // default constructor, it doesn't navigate to whatfix url and login
- public BaseTest() {
- }
- // initializes the url and other params
- private void init(String path) {
- this.path = path;
- this.url = data.getWhatfixUrl() + path;
- this.jenkinsUrl = data.getWhatfixUrl() + "automation3";
- this.isLoginLogoutRequired = true;
- }
- // constructor which will support navigating to whatfix url with {path} param
- public BaseTest(String path) {
- init(path);
- }
- // constructor which will support navigating to whatfix url with {path} param and passed username and password
- public BaseTest(String path, String username, String password) {
- this.username = username;
- this.password = password;
- init(path);
- }
- // logs in to the app
- @BeforeMethod
- public void handleTestMethodName(Method method) {
- System.out.println("--------------------------------------------------------------------------------------");
- System.out.println("--------------------------------------------------------------------------------------");
- System.out.println("Starting test: " + getClass().getSimpleName() + " --> " + method.getName());
- System.out.println("--------------------------------------------------------------------------------------");
- System.out.println("--------------------------------------------------------------------------------------");
- if (isLoginLogoutRequired) {
- navigateToWhatfixUrl();
- login();
- }
- }
- protected void navigateToWhatfixUrl() {
- navigateTo(url, true);
- }
- protected void navigateTo(String url) {
- navigateTo(url, false);
- }
- protected void navigateTo(String url, boolean isWhatfixUrl) {
- String navigationUrl = null;
- if (isWhatfixUrl) {
- if (StringUtils.isEmpty(System.getProperty("env"))) {
- navigationUrl = url;
- } else if (StringUtils.equalsIgnoreCase(System.getProperty("env"), "jenkins")) {
- navigationUrl = jenkinsUrl;
- }
- } else {
- navigationUrl = url;
- }
- System.out.println("Navigating to URL --> " + navigationUrl);
- driver.get(navigationUrl);
- }
- protected void login() {
- if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
- if (path.equals("automation1")) {
- flowsBeforeAddition = loginAndGetFlowCount(data.getUsername(), data.getPassword());
- } else if (path.equals("automation2")) {
- flowsBeforeAddition = loginAndGetFlowCount(data.getmyUsername(), data.getmyPassword());
- } else if (path.equals("whatfix-uat-2")) {
- flowsBeforeAddition = loginAndGetFlowCount(data.getUsername(), data.getPassword());
- }
- } else {
- flowsBeforeAddition = loginAndGetFlowCount(username, password);
- }
- }
- private int loginAndGetFlowCount(String username, String password) {
- WhatfixLoginPage whatfixLoginPage = PageFactory.initElements(driver, WhatfixLoginPage.class);
- return whatfixLoginPage
- .enterUsername(username)
- .enterPassword(password)
- .submit()
- .getFlowCount();
- }
- protected void launchWhatfixPlugin() {
- JavascriptExecutor js = (JavascriptExecutor) driver;
- String sc = "var script = document.createElement('script');\n" +
- "script.type = 'text/javascript';\n" +
- "script.src = '//whatfix.com/gluexsi/gluexsi.nocache.js';\n" +
- "document.head.appendChild(script);";
- js.executeScript(sc);
- }
- private BaseTest bringToForeground() {
- String currentWindowHandle = driver.getWindowHandle();
- ((JavascriptExecutor) driver).executeScript("alert('Test')");
- driver.switchTo().alert().accept();
- driver.switchTo().window(currentWindowHandle);
- return this;
- }
- private void maximize() {
- System.out.println("Maximizing browser...");
- driver.manage().window().maximize();
- }
- private BaseTest enableWhatfixExtension() {
- ChromeOptions options = new ChromeOptions();
- try {
- options.addExtensions(new File(this.getClass().getClassLoader().getResource("whatfix_v4.1.crx").toURI()));
- Map<String, Object> prefs = new HashMap<>();
- prefs.put("credentials_enable_service", false);
- prefs.put("password_manager_enabled", false);
- options.setExperimentalOption("prefs", prefs);
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- capabilities.setCapability(ChromeOptions.CAPABILITY, options);
- return this;
- }
- private BaseTest setBrowser() {
- System.out.println("Launching " + config.getBrowser().toUpperCase() + " browser.");
- if (config.getBrowser().equals("chrome")) {
- capabilities = DesiredCapabilities.chrome();
- }
- return this;
- }
- @AfterMethod
- public void logout() {
- System.out.println("Starting after method...");
- if (isLoginLogoutRequired) {
- WhatfixLandingPage whatfixLandingPage = PageFactory.initElements(driver, WhatfixLandingPage.class);
- System.out.println("Logging out...");
- navigateTo(url);
- whatfixLandingPage
- .settings()
- .logout();
- }
- System.out.println("");
- System.out.println("");
- System.out.println("");
- }
- @AfterSuite
- public void tearDown() {
- System.out.println("Starting after suite...");
- driver.quit();
- }
- }
Add Comment
Please, Sign In to add comment