简体   繁体   中英

Run Webdriver Junit browser specific test cases

I have selenium.properties in which i specify the test configuration (baseURL, browser etc). This is used by ant script to kick off webdriver junit test cases. Now I have few junit test methods, that i want to run only on Firefox. I was wondering if there a way i can accomplish this using JUnit annotations? can i create custom annotations?

my setup

public class TestBase{
    public static String baseURL = null;
    public static String browser = null;

    @BeforeClass
    public static void webdriverSetUp() {

        try {

            FileInputStream fn = new FileInputStream(SELENIUM_PROP_FILE);
            Properties selenium_properties = new Properties();
            selenium_properties.load(fn);                       
            baseURL = selenium_properties.getProperty("baseUrl");
            browser = selenium_properties.getProperty("browser");         
              } catch (Exception e) {
            e.printStackTrace();
        }
         if(browserg.equalsIgnoreCase("firefox")){
                File profileDirectory = new File("./profile");              
                FirefoxProfile profile = new FirefoxProfile(profileDirectory);

                driver = new FirefoxDriver(profile);                
            }
}

//Test Class

    public class TestCase1 extends TestBase{

       @Test //run this case only if browser = firefox
       public void test1(){
       }
       @Test //do not run this case if browser = chrome
       public void test2(){
       }
    }
any pointers?

You can easily do this with JUnit with your own runner. In fact, there is a similar working code in Selenium WebDriver test - it's just backwards. The Selenium guys wanted to skip some tests for particular browsers, so they introduced a custom @Ignore annotation.

Take a look at JUnit4TestBase , SeleniumTestRunner and finally TestIgnorance .

You can use their idea to make the opposite and only run the tests with the desired drivers. However, I think you'll need to write it yourself as I am not aware of any good solution out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM