Question

I have a unique situation where I have to set alwaysRun = true on my setup and teardown methods for one browser only (IE8) due to the way Selenium and the current framework handle things. I could go into my reasoning but I'll just skip to the question for now:

Is there a way that can get the alwaysRun = true tags to be ignored if I pass in a particular system property or somehow parameterize these annotations so they're only interpreted as true under certain circumstances? I'm using TestNG, Gradle, and Java driving Selenium 2.

Here's the bulk of my base test class (extended by all my tests):

   @BeforeClass(alwaysRun = true)
    public void suiteSetup() throws Exception {
        if (REMOTE_DRIVER) {
            DesiredCapabilities capabilities;
            if (BROWSER.equals("firefox")) {
                capabilities = DesiredCapabilities.firefox();
            } else if (BROWSER.equals("internetExplorer")) {
                capabilities = DesiredCapabilities.internetExplorer();
                capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            } else if (BROWSER.equals("chrome")) {
                capabilities = DesiredCapabilities.chrome();
            } else {
                throw new RuntimeException("Browser type unsupported");
            }
            driver = new RemoteWebDriver(
                    new URL("http://" + SELENIUM_HOST + ":" + SELENIUM_PORT + "/wd/hub"),
                    capabilities);
            driver.setFileDetector(new LocalFileDetector());
            //  driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
            driver.manage().window().maximize();
        } else {
            if (BROWSER.equals("firefox")) {
                driver = new FirefoxDriver();
            } else if (BROWSER.equals("chrome")) {
                String path;
                if (System.getProperty("os.name").contains("Windows")) {
                    path = "lib/chromedriver.exe";
                } else {
                    path = "lib/chromedriver";
                }
                System.setProperty("webdriver.chrome.driver", path);
                driver = new ChromeDriver();
                // on linux, you may have to uncomment these, I couldn't get it to work without it
                //ChromeOptions options = new ChromeOptions();
                //options.setBinary(new File("/usr/lib/chromium-browser/chromium-browser"));
                //driver = new ChromeDriver(options);
            } else if (BROWSER.equals("internetExplorer")) {
                DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
                capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                driver = new InternetExplorerDriver(capabilities);
            } else {
                throw new RuntimeException("Browser type unsupported");
            }
        }
    }

    @BeforeMethod(alwaysRun = true)
    public void loadLoginPage() {
        try {
            driver.get("http://" + WEB_SERVER);
        } catch (Exception e) {
            try {
                // some types of errors may leave a hanging "are you sure you want to leave this page" dialog
                // accept if it exists, ignore it otherwise
                driver.switchTo().alert().accept();
            } catch (WebDriverException ex) {
                // no alert
            }
            throw new RuntimeException(e);
        }
    }

    @AfterMethod(alwaysRun = true)
    public void testTearDown() {
        try {
            // some types of errors may leave a hanging "are you sure you want to leave this page" dialog
            // accept if it exists, ignore it otherwise
            driver.switchTo().alert().accept();
        } catch (NoAlertPresentException e) {
            // no alert
        }
        driver.manage().deleteAllCookies();
    }

    @AfterClass(alwaysRun = true)
    public void suiteTearDown() {
        driver.quit();
    }

Without going nuts and refactoring this entire project, what would be the best way for me to go about "using" simply @BeforeClass for all my runs except IE which requires @BeforeClass(alwaysRun = true) (and the rest of the setup and teardown methods) with the least amount of headache? I've considered many options but they're all massive undertakings and I have a feeling there has to be a simple solution out there.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top