package abstract_classes; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Map.Entry; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.htmlunit.HtmlUnitDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.Wait; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import qsf_page_object_pattern.BackEnd.BackEnd_CmsPage; import qsf_page_object_pattern.BackEnd.BackEnd_LoginPage; import tools.Settings; public abstract class BaseTest extends Assert{ protected WebDriver driver; protected Settings settings = new Settings(); final String EmptyFieldMessage = "Value is required and can't be empty"; final String DefaultAlertMessage = "Please review the comments marked in red and make appropriate corrections."; final String PasswordConfirmationAlert = "Password confirmation does not match"; final String EmailWrongFormatAlert = "Email is in wrong format"; final String UniqueEmailAlert = "Email must be unique"; final String UniqueLoginAlert = "Login must be unique"; protected void SetDriver(String browser){ switch ( browser ) { case "Firefox": this.driver = new FirefoxDriver(); break; case "Iexplorer": { File file = new File(System.getProperty("user.dir")+"/drivers/IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); this.driver = new InternetExplorerDriver(); } break; case "Chrome": { File file = new File(System.getProperty("user.dir")+"/drivers/chromedriver.exe"); System.setProperty("webdriver.chrome.driver", file.getAbsolutePath()); this.driver = new ChromeDriver(); } break; case "Remote": { try { this.driver = new RemoteWebDriver(new URL("http://selenium.dev.adaptainc.com:4444/wd/hub"), DesiredCapabilities.firefox()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; case "Safari": { this.driver = new SafariDriver(); }break; case "HtmlUnit": { this.driver = new HtmlUnitDriver(true); //((HtmlUnitDriver) this.driver).setJavascriptEnabled(true); }break; default:this.driver = new FirefoxDriver(); } } protected BackEnd_CmsPage loginToAdmin(){ driver.get(settings.http + ((settings.httpUser.length()>0) ? (settings.httpUser + ":" + settings.httpPassword + "@") : "") + settings.adminUrl); BackEnd_LoginPage be_loginpage = new BackEnd_LoginPage(driver); return be_loginpage.dologin(settings.adminLogin, settings.adminPassword); } protected void clearCookies(){ driver.manage().deleteAllCookies(); //driver.get(this.http+this.adminUrl+"login"); } protected void compareFormErrors(Map>realErrors, Map>expectedErrors){ for(Entry> realFieldErrors : realErrors.entrySet()) { String fieldName = realFieldErrors.getKey(); for(String realFieldError : realFieldErrors.getValue()){ if(expectedErrors.containsKey(fieldName)){ assertTrue(expectedErrors.get(fieldName).contains(realFieldError), "Unexpected error was found on '"+fieldName+"' with message: '"+realFieldError+"'."); } else{ throw new NoSuchElementException("Unexpected errors was found on '"+fieldName+"'. Found errors: "+realFieldErrors.getValue()); } } } for(Entry> expectedFieldErrors : expectedErrors.entrySet()) { String fieldName = expectedFieldErrors.getKey(); for(String expectedFieldError : expectedFieldErrors.getValue()){ if(realErrors.containsKey(fieldName)){ assertTrue(realErrors.get(fieldName).contains(expectedFieldError), "Expecting error for '"+fieldName+"' with message: '"+expectedFieldError+"'. Not Found."); } else{ throw new NoSuchElementException("Expecting errors on '"+fieldName+"'. Expected errors: "+expectedFieldErrors.getValue() + " Errors Not Found"); } } } } public void makeElementVisibleByJavascript(final WebElement element) { String script = "var element = arguments[0];" + "element.style.display='block';" ; ((JavascriptExecutor)driver).executeScript(script, element); } public static Map> getQueryParams(String url) { try { Map> params = new HashMap>(); String[] urlParts = url.split("\\?"); if (urlParts.length > 1) { String query = urlParts[1]; for (String param : query.split("&")) { String[] pair = param.split("="); String key = URLDecoder.decode(pair[0], "UTF-8"); String value = ""; if (pair.length > 1) { value = URLDecoder.decode(pair[1], "UTF-8"); } List values = params.get(key); if (values == null) { values = new ArrayList(); params.put(key, values); } values.add(value); } } return params; } catch (UnsupportedEncodingException ex) { throw new AssertionError(ex); } } public void echo(Object data){ System.out.println(data); } public void waitForPageLoaded() { try { Thread.sleep(800); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } ExpectedCondition expectation = new ExpectedCondition() { public Boolean apply(WebDriver driver) { return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"); } }; Wait wait = new WebDriverWait(driver,30); try { wait.until(expectation); } catch(Throwable error) { Assert.assertFalse(true,"Timeout waiting for Page Load Request to complete."); } } }