running selenium webdriver test cases against multiple browsers

后端 未结 3 1580
野性不改
野性不改 2021-02-02 03:03

I am new to selenium testing. I want to run selenium test cases on multiple browsers against internet explorer, Firefox, opera and chrome. What approach i have to f

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 03:53

    You could use the WebDriver Extensions framework's JUnitRunner

    Here is an example test googling for "Hello World"

    @RunWith(WebDriverRunner.class)
    @Firefox
    @Chrome
    @InternetExplorer
    public class WebDriverExtensionsExampleTest {
    
        // Model
        @FindBy(name = "q")
        WebElement queryInput;
        @FindBy(name = "btnG")
        WebElement searchButton;
        @FindBy(id = "search")
        WebElement searchResult;
    
        @Test
        public void searchGoogleForHelloWorldTest() {
            open("http://www.google.com");
            assertCurrentUrlContains("google");
    
            type("Hello World", queryInput);
            click(searchButton);
    
            waitFor(3, SECONDS);
            assertTextContains("Hello World", searchResult);
        }
    }
    

    just make sure to add the WebDriver Extensions framework amongst your maven pom.xml dependencies

    
        com.github.webdriverextensions
        webdriverextensions
        1.2.1
    
    

    The drivers can be downloaded using the provided maven plugin. Simply add

    
        com.github.webdriverextensions
        webdriverextensions-maven-plugin
        1.0.1
        
            
                
                    install-drivers
                
            
        
        
            
                
                    internetexplorerdriver
                    2.44
                
                
                    chromedriver
                    2.12
                
            
        
    
    

    to your pom.xml. Or if you prefer downloading them manually just annotate the test class with the

    @DriverPaths(chrome="path/to/chromedriver", internetExplorer ="path/to/internetexplorerdriver")
    

    annotation pointing at the drivers.

    Note that the above example uses static methods from the WebDriver Extensions Bot class to make the test more readable. However you are not tied to using them. The above test rewritten in pure Selenium WebDriver would look like this

        @Test
        public void searchGoogleForHelloWorldTest() throws InterruptedException {
            WebDriver driver = WebDriverExtensionsContext.getDriver();
    
            driver.get("http://www.google.com");
            assert driver.getCurrentUrl().contains("google");
    
            queryInput.sendKeys("Hello World");
            searchButton.click();
    
            SECONDS.sleep(3);
            assert searchResult.getText().contains("Hello World");
        }
    

提交回复
热议问题