Search in sources :

Example 1 with InvalidBrowserException

use of com.coveros.selenified.exceptions.InvalidBrowserException in project selenified by Coveros.

the class Selenified method startTest.

/**
 * Gathers all of the testing information, and setup up the logging. If a
 * selenium test is running, also sets up the webdriver object
 *
 * @param dataProvider - any objects that are being passed to the tests to loop
 *                     through as variables
 * @param method       - what is the method that is being run. the test name will be
 *                     extracted from this
 * @param test         - was the is context associated with this test suite. suite
 *                     information will be extracted from this
 * @param result       - where are the test results stored. browser information will
 *                     be kept here
 * @param selenium     - is this a selenium test. if so, the webdriver content will
 *                     be setup
 */
protected void startTest(Object[] dataProvider, Method method, ITestContext test, ITestResult result, DriverSetup selenium) {
    String testName = TestSetup.getTestName(method, dataProvider);
    String outputDir = test.getOutputDirectory();
    String extClass = method.getDeclaringClass().getName();
    String description = "";
    String group = "";
    Test annotation = method.getAnnotation(Test.class);
    // set description from annotation
    if (annotation.description() != null) {
        description = annotation.description();
    }
    // adding in the group if it exists
    if (annotation.groups() != null) {
        group = Arrays.toString(annotation.groups());
        group = group.substring(1, group.length() - 1);
    }
    while (test.getAttribute(testName + INVOCATION_COUNT) == null) {
        test.setAttribute(testName + INVOCATION_COUNT, 0);
    }
    int invocationCount = (int) test.getAttribute(testName + INVOCATION_COUNT);
    Browser myBrowser = browsers.get(invocationCount);
    if (!selenium.useBrowser()) {
        myBrowser = Browser.NONE;
    }
    DesiredCapabilities myCapability = capabilities.get(invocationCount);
    myCapability.setCapability("name", testName);
    this.capability.set(myCapability);
    OutputFile myFile = new OutputFile(outputDir, testName, myBrowser, getTestSite(extClass, test), test.getName(), group, getAuthor(extClass, test), getVersion(extClass, test), description);
    if (selenium.useBrowser()) {
        App app = null;
        try {
            app = new App(myBrowser, myCapability, myFile);
        } catch (InvalidBrowserException | MalformedURLException e) {
            log.error(e);
        }
        this.apps.set(app);
        this.calls.set(null);
        myFile.setApp(app);
        if (selenium.loadPage()) {
            loadInitialPage(app, getTestSite(extClass, test), myFile);
        }
    } else {
        HTTP http = new HTTP(getTestSite(extClass, test), servicesUser, servicesPass);
        Call call = new Call(http, myFile, extraHeaders);
        this.apps.set(null);
        this.calls.set(call);
    }
    this.browser.set(myBrowser);
    result.setAttribute(BROWSER_INPUT, myBrowser);
    this.files.set(myFile);
}
Also used : App(com.coveros.selenified.application.App) Call(com.coveros.selenified.services.Call) MalformedURLException(java.net.MalformedURLException) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) HTTP(com.coveros.selenified.services.HTTP) InvalidBrowserException(com.coveros.selenified.exceptions.InvalidBrowserException)

Example 2 with InvalidBrowserException

use of com.coveros.selenified.exceptions.InvalidBrowserException in project selenified by Coveros.

the class ExceptionTest method invalidBrowserExceptionTest.

@Test
public void invalidBrowserExceptionTest() {
    try {
        TestSetup.setupDriver(Browser.ANDROID, new DesiredCapabilities());
        Assert.fail("Expected an InvalidBrowserException");
    } catch (InvalidBrowserException e) {
        Assert.assertEquals(e.getMessage(), "The selected browser ANDROID is not an applicable choice");
    }
}
Also used : DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) InvalidBrowserException(com.coveros.selenified.exceptions.InvalidBrowserException) Test(org.testng.annotations.Test)

Example 3 with InvalidBrowserException

use of com.coveros.selenified.exceptions.InvalidBrowserException in project selenified by Coveros.

the class TestSetup method setupDriver.

/**
 * this creates the webdriver object, which will be used to interact with
 * for all browser web tests
 *
 * @param browser      - what browser is being tested on
 * @param capabilities - what capabilities are being tested with
 * @return WebDriver: the driver to interact with for the test
 * @throws InvalidBrowserException If a browser that is not one specified in the
 *                                 Selenium.Browser class is used, this exception will be thrown
 */
public static WebDriver setupDriver(Browser browser, DesiredCapabilities capabilities) throws InvalidBrowserException {
    WebDriver driver;
    // check the browser
    switch(browser) {
        case HTMLUNIT:
            capabilities.setBrowserName("htmlunit");
            capabilities.setJavascriptEnabled(true);
            System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "fatal");
            java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
            java.util.logging.Logger.getLogger("org.apache.http").setLevel(Level.OFF);
            driver = new HtmlUnitDriver(capabilities);
            break;
        case FIREFOX:
            FirefoxDriverManager.getInstance().forceCache().setup();
            FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                firefoxOptions.setHeadless(true);
            }
            driver = new FirefoxDriver(firefoxOptions);
            break;
        case CHROME:
            ChromeDriverManager.getInstance().forceCache().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions = chromeOptions.merge(capabilities);
            if (System.getProperty(HEADLESS_INPUT) != null && "true".equals(System.getProperty(HEADLESS_INPUT))) {
                chromeOptions.setHeadless(true);
            }
            driver = new ChromeDriver(chromeOptions);
            break;
        case INTERNETEXPLORER:
            InternetExplorerDriverManager.getInstance().forceCache().setup();
            InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions(capabilities);
            driver = new InternetExplorerDriver(internetExplorerOptions);
            break;
        case EDGE:
            EdgeDriverManager.getInstance().forceCache().setup();
            EdgeOptions edgeOptions = new EdgeOptions();
            edgeOptions = edgeOptions.merge(capabilities);
            driver = new EdgeDriver(edgeOptions);
            break;
        case SAFARI:
            SafariOptions safariOptions = new SafariOptions(capabilities);
            driver = new SafariDriver(safariOptions);
            break;
        case OPERA:
            OperaDriverManager.getInstance().forceCache().setup();
            driver = new OperaDriver(capabilities);
            break;
        case PHANTOMJS:
            PhantomJsDriverManager.getInstance().forceCache().setup();
            driver = new PhantomJSDriver(capabilities);
            break;
        // if the browser is not listed, throw an error
        default:
            throw new InvalidBrowserException("The selected browser " + browser + " is not an applicable choice");
    }
    return driver;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) PhantomJSDriver(org.openqa.selenium.phantomjs.PhantomJSDriver) OperaDriver(org.openqa.selenium.opera.OperaDriver) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) InternetExplorerOptions(org.openqa.selenium.ie.InternetExplorerOptions) SafariOptions(org.openqa.selenium.safari.SafariOptions) SafariDriver(org.openqa.selenium.safari.SafariDriver) FirefoxOptions(org.openqa.selenium.firefox.FirefoxOptions) EdgeOptions(org.openqa.selenium.edge.EdgeOptions) EdgeDriver(org.openqa.selenium.edge.EdgeDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) InvalidBrowserException(com.coveros.selenified.exceptions.InvalidBrowserException)

Aggregations

InvalidBrowserException (com.coveros.selenified.exceptions.InvalidBrowserException)3 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)2 App (com.coveros.selenified.application.App)1 Call (com.coveros.selenified.services.Call)1 HTTP (com.coveros.selenified.services.HTTP)1 MalformedURLException (java.net.MalformedURLException)1 WebDriver (org.openqa.selenium.WebDriver)1 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)1 ChromeOptions (org.openqa.selenium.chrome.ChromeOptions)1 EdgeDriver (org.openqa.selenium.edge.EdgeDriver)1 EdgeOptions (org.openqa.selenium.edge.EdgeOptions)1 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)1 FirefoxOptions (org.openqa.selenium.firefox.FirefoxOptions)1 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)1 InternetExplorerDriver (org.openqa.selenium.ie.InternetExplorerDriver)1 InternetExplorerOptions (org.openqa.selenium.ie.InternetExplorerOptions)1 OperaDriver (org.openqa.selenium.opera.OperaDriver)1 PhantomJSDriver (org.openqa.selenium.phantomjs.PhantomJSDriver)1 SafariDriver (org.openqa.selenium.safari.SafariDriver)1 SafariOptions (org.openqa.selenium.safari.SafariOptions)1