1

嗨,我是 Webdriver 和 Junit 的新手。需要解决这个难题。我做了很多研究,但找不到可以帮助的人。

所以我将我的驱动程序声明为静态 Webdriver fd,然后尝试在单个类文件上运行案例(也尝试了多个类文件),但每次运行浏览器都会启动两次。我似乎可以调试这个问题,请帮忙。这是我的代码:

package Wspace;

import java.io.IOException;  
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Wsinventory {

    static WebDriver fd;

    @Rule
    public static ErrorCollector errCollector = new ErrorCollector();

    @Before
    public void openFirefox() throws IOException 
    {        
        System.out.println("Launching WebSpace 9.0 in FireFox");

        ProfilesIni pr = new ProfilesIni();
        FirefoxProfile fp = pr.getProfile("ERS_Profile"); //FF profile

        fd = new FirefoxDriver(fp);
        fd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

        fd.get("http://www.testsite.com");
        fd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        String ffpagetitle = fd.getTitle(); //ffpagetitle means firefox page title
        //System.out.println(pagetitle);
        try{
        Assert.assertEquals("Webspace Login Page", ffpagetitle);
        }catch(Throwable t){
            System.out.println("ERROR ENCOUNTERED");
            errCollector.addError(t);
        }

    }       

    @Test
    public void wsloginTest(){

        fd.findElement(By.name("User ID")).sendKeys("CA");
        fd.findElement(By.name("Password")).sendKeys("SONIKA");
        fd.findElement(By.name("Logon WebSpace")).click();
    }

    @Test
    public void switchtoInventory() throws InterruptedException{

         Thread.sleep(5000);
         fd.switchTo().frame("body"); //Main frame
         fd.switchTo().frame("leftNav"); //Sub frame
         fd.findElement(By.name("Inventory")).click();
         fd.switchTo().defaultContent();
         fd.switchTo().frame("body");
    }


}
4

1 回答 1

3

问题是您的@Before方法在每次测试之前都在运行。当您运行第二个测试时,它会再次调用该方法,并丢弃旧的 FirefoxDriver 实例,并创建一个新实例(存储在该驱动程序的静态实例中)。

改为使用@BeforeClass

于 2013-08-02T17:57:47.817 回答