使用构建或环境参数更改/忽略Gradle中的TestNG注释?

时间:2014-03-01 00:51:29

标签: java selenium gradle testng

由于Selenium和当前框架的处理方式,我有一个独特的情况,我必须在我的设置和拆解方法上设置alwaysRun = true仅限一个浏览器(IE8)。我可以进入我的推理,但我现在就跳过这个问题:

如果我传入特定的系统属性或以某种方式参数化这些注释以便在某些情况下它们只被解释为alwaysRun = true,是否有一种方法可以使true标记被忽略?我正在使用TestNG,Gradle和Java来驱动Selenium 2.

这是我的基础测试类的大部分(由我的所有测试扩展):

   @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();
    }

如果没有疯狂并重构整个项目,那么除了需要@BeforeClass的IE之外,对于我的所有运行来说,仅仅@BeforeClass(alwaysRun = true)“使用”的最佳方式是什么(以及其余的最少头痛的设置和拆解方法?我考虑了很多选择,但它们都是大规模的事业,我觉得必须有一个简单的解决方案。

0 个答案:

没有答案