English 中文(简体)
开放式日子试验 不同浏览器使用“自动”
原标题:Running JUnit Tests On different browsers using Selenium "Automatically"

因此,使用 se二氧化物,并且正在成功地利用它来测试火f和hr。 但是,我需要做的是自动对这两个浏览器进行同样的单位测试。 I ve试图将网络驱动器置于ArrayList<Web/63/7r>司机标的栏目中,但如果我这样做,则试卷没有正确操作。 目前,只有一名司机在ArrayList,但该驾驶员不会只工作一次。 这里的一些法典......

package testsuites;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BingTests extends BaseTestSuite{
//private WebDriver fireFoxDriver;
private WebDriver chromeDriver;
private WebDriver fireFoxDriver;
private  ArrayList<WebDriver> drivers;

@Before
public void setUp() throws Exception {
    fireFoxDriver = new FirefoxDriver();
    fireFoxDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    drivers.add(fireFoxDriver);
}

@Test
public void testGoogle(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Selenium IDE");
        driver.findElement(By.id("sb_form_go")).click();
          driver.findElement(By.xpath("//ul[@id= wg0 ]/li[2]/div/div/h3/a/strong")).click();
        WebElement elem = driver.findElement(By.id("mainContent")); 
        assertTrue(elem.getText().contains("Selenium News"));
    }
}

@Test
public void isWikiContentCorrect(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Saturn");
        driver.findElement(By.id("sb_form_go")).click();
        driver.findElement(By.linkText("Saturn - Wikipedia, the free encyclopedia")).click();
        driver.findElement(By.cssSelector("li.toclevel-1.tocsection-9 > a > span.toctext")).click();
        assertTrue(driver.getPageSource().contains("53 of which"));
    }
}

@Test
public void isWikiTitleCorrect(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Saturn");
        driver.findElement(By.id("sb_form_go")).click();
        driver.findElement(By.linkText("Saturn - Wikipedia, the free encyclopedia")).click();
        assertEquals("Saturn - Wikipedia, the free encyclopedia", driver.getTitle());
    }
}

@Test
public void testDropDownWithSelenium(){
    for(WebDriver driver: drivers){
        driver.get("http://www.bing.com");
        driver.findElement(By.id("sb_form_q")).clear();
        driver.findElement(By.id("sb_form_q")).sendKeys("Neptunes moon");
        driver.findElement(By.partialLinkText("moons and rings")).click();
        driver.findElement(By.linkText("Neptune (planet) :: Neptune s moons       and rings -- Britannica Online ...")).click();
        List<WebElement> elems = driver.findElements(By.tagName("Input"));
        for(WebElement elem: elems){
            System.out.println(elem.getText());
        }
    }
}

@After
public void tearDown() throws Exception {
    for(WebDriver driver: drivers){
        driver.close();
    }
}
}
最佳回答

你采用了错误的做法。 这种执行需要在测试方法中采用不必要的排位法。 随着测试病例的增加,这类做法更加痛苦(见100个测试案例)。

Use Selenium Grid or utilise QAF formerly ISFW. In Qmetry Automation Framework (QAF) you can set it in configuration file to run against different browsers. For example

<suite name="Test Automation" verbose="0" parallel="tests">

    <test name="Test on FF">
      <parameter name="browser" value="InternetExplorerWebDriver" />
    ...
    </test>
    <test name="Test on IE">
      <parameter name="browser" value="firefoxWebDriver" />
    ...
    </test>
</suit>
问题回答

JUNIT with Selenium on Different Browsers

Launching different browsers ex:ook &fireFox auto

public class SimpleJunit {
    private static WebDriver driver;
    public static int random = 0;
    private String baseURL;
    // @BeforeClass : Executes only once for the Test-Class. 
    @BeforeClass public static void setting_SystemProperties(){
        System.out.println("System Properties seting Key value.");

        System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe");  // Chrome Driver Location.
    }
    // @Before      : To execute once before ever Test.
    @Before public void test_Setup(){       
        System.out.println("Launching Browser");
        if (random == 0) {
            driver = new ChromeDriver(); // Creates new SessionID & opens the Browser.
        }else {
            driver = new FirefoxDriver();
        }

        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        System.out.println("Session ID : " + ((RemoteWebDriver) driver).getSessionId() );
    }
    //  @Test        : Testing senarios.
    @Test public void robot_ScreenShot() throws AWTException, IOException{
        System.out.println("Robot Tset Screen Shot.");  
        baseURL = "http://searchsoa.techtarget.com/definition/stickiness";
                Robot robot = new Robot();   // CTRL+T new tab in Browser.                   

        driver.get(baseURL);        

                Toolkit toolkit = Toolkit.getDefaultToolkit();  
                int width = (int) toolkit.getScreenSize().getWidth();  
                int height = (int) toolkit.getScreenSize().getHeight();                 

                Rectangle area = new Rectangle(0, 0, width, height);
                BufferedImage bufferedImage = robot.createScreenCapture(area);
                ImageIO.write(bufferedImage, "png", new File("D:\Screenshots\JUNIT-Robot.png"));
                random += 1;
    }
    @Test public void selenium_ScreenShot() throws IOException { 
        baseURL = "http://www.w3schools.com/css/css_positioning.asp";
        driver.get(baseURL);
        System.out.println("Selenium Screen shot.");
        File screenshotFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);                        
        FileUtils.copyFile(screenshotFile, new File("D:\Screenshots\JUNIT-Selenium.jpg"));
        random += 1;
    }
    // @After       : To execute once after ever Test.
    @After  public void test_Cleaning(){
        System.out.println("Closing Browser");
        baseURL = null;     
        driver.close(); // Removing SessionID & Close the Browser.      
/*if you are not using driver.quit(). then JUNIT-Test will not terminate untill you end chromedriver.exe process
  in Task-Manager. use CTRL+SHIFT+ESC to open TaskManager then goto processes-view find chromedriver.exe and 
  then end the process manually. */ 
        driver.quit(); // Ends the Process by removing chromedriver.exe process from Task-Manager.
    }
    // @AfterClass  : Executes only once before Terminating the Test-Class.
    @AfterClass public static void clearing_SystemProperties(){
        System.out.println("System Property Removing Key value.");              
        System.clearProperty("webdriver.chrome.driver");
    }   
}

JUnit 测试执行令:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...