两个空的firefox浏览器实例打开了testng selenium webdriver

时间:2015-06-10 08:29:24

标签: selenium webdriver testng

我一直有这个问题,当我运行一个testng测试套件firefox打开三个浏览器实例并在第三个实例上执行我的测试时,前两个实例完全为空,我希望我的firefox驱动程序要做的就是创建浏览器的一个实例并在其上执行我的测试。我的testing.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNG Suite" parallel="false">

    <test name="Firefox Test">

        <parameter name="browserName" value="firefox"></parameter>

         <classes>
            <class name="Publisher.CreateSites" />

        </classes>

    </test> <!-- Test -->

     <test name="Chrome Test">

        <parameter name="browserName" value="chrome"></parameter>

         <classes>
            <class name="Publisher.CreateSites" />

        </classes>

    </test> <!-- Test -->
</suite> <!-- Suite -->

我的java类看起来像

public class CreateSites 
{

    private ReadExcel fr = new ReadExcel();

    public WebDriver driver = new FirefoxDriver();

    @BeforeTest
    @Parameters("browserName")

    public void setup(String browserName) throws Exception{

        //Check if parameter passed from TestNG is 'firefox'

        if(browserName.equalsIgnoreCase("firefox"))
        {

            //create firefox instance
            driver = new FirefoxDriver();

        }

        //Check if parameter passed as 'chrome'

        else if(browserName.equalsIgnoreCase("chrome"))
        {

            //set path to chromedriver.exe You may need to download it from http://code.google.com/p/selenium/wiki/ChromeDriver
            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");

            //create chrome instance
            driver = new ChromeDriver();

        }

        else
        {
            //If no browser passed throw exception
            throw new Exception("Browser is not correct");
        }

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://portal.admaxim.com/main3/home.jsf");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

    }

     @Test(priority=0)
      public void loginTest()
      {
            fr = new ReadExcel();
            fr.readexel("TestNGExcelFile", "Create_Site");
            driver.findElement(By.id("loginForm:userName")).click();
            driver.findElement(By.id("loginForm:userName")).clear();
            driver.findElement(By.id("loginForm:userName")).sendKeys(fr.List1.get(0));

            driver.findElement(By.id("loginForm:password")).click();
            driver.findElement(By.id("loginForm:password")).clear();
            driver.findElement(By.id("loginForm:password")).sendKeys(fr.List1.get(1));
            driver.findElement(By.id("loginForm:submit")).click();
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
      }
     @Test(priority=1)
     public void moveToPublisher() throws InterruptedException
     {
         driver.findElement(By.xpath(".//*[@id='homeForm:menuPublisherOption1']/img")).click();
         Thread.sleep(4000);
     }
     @Test(priority=2)
     public void clickOnCreateSiteLink()
     {
         driver.findElement(By.xpath(".//*[@id='deliveryImg']")).click();
         driver.findElement(By.xpath(".//*[@id='homeForm:welcomePubView:createSiteLbl']")).click();
         driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
     }
     @Test(priority=3)
     public void selectAccount() throws InterruptedException
     {
         Thread.sleep(3000);
         Select account = new Select(driver.findElement(By.xpath(".//*[@id='availableSubAccountsList']")));
         account.selectByVisibleText("droidpubsub");
         driver.findElement(By.xpath(".//*[@id='selectSubAccount_div']/table/tbody/tr[2]/td[2]/input[1]")).click();
     }
     @Test(priority=4)
     public void selectBasicInfo()
     {
         driver.findElement(By.xpath(".//*[@id='accordion']/ul/li[2]/a")).click();

         driver.findElement(By.id("categoryForm:name")).click();
         driver.findElement(By.id("categoryForm:name")).clear();
         driver.findElement(By.id("categoryForm:name")).sendKeys(fr.List1.get(2));

         driver.findElement(By.id("categoryForm:externalSiteID")).click();
         driver.findElement(By.id("categoryForm:externalSiteID")).clear();
         driver.findElement(By.id("categoryForm:externalSiteID")).sendKeys(fr.List1.get(3));

         driver.findElement(By.id("categoryForm:siteURL")).click();
         driver.findElement(By.id("categoryForm:siteURL")).clear();
         driver.findElement(By.id("categoryForm:siteURL")).sendKeys(fr.List1.get(4));

         driver.findElement(By.id("categoryForm:description")).click();
         driver.findElement(By.id("categoryForm:description")).clear();
         driver.findElement(By.id("categoryForm:description")).sendKeys(fr.List1.get(5));

     }
     @Test(priority=5)
     public void setTargeting() throws InterruptedException
     {
         driver.findElement(By.xpath(".//*[@id='accordion']/ul/li[3]/a")).click();
         Thread.sleep(2000);

         // Select values from Demographic tab
         // Select Gender.
         WebElement selectGender = driver.findElement(By.id("categoryForm:demographics_avail_gender"));
         Select genderOptions = new Select(selectGender);
         for(int i= 0; i<2; i++)
         {
         genderOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='genderDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

         // Select Age.
         driver.findElement(By.xpath(".//*[@id='ageDemographicTabBtnTd']")).click();
         WebElement selectAge = driver.findElement(By.id("categoryForm:demographics_avail_age"));
         Select Ageoptions = new Select(selectAge);
         for(int i= 0; i<5; i++)
         {
             Ageoptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='ageDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

         // Select Occupation type.
         driver.findElement(By.xpath(".//*[@id='occupationTypeDemographicTabBtnTd']/a")).click();
         WebElement selectOccupationType = driver.findElement(By.id("categoryForm:demographics_avail_occupationType"));
         Select occupationTypeOptions = new Select(selectOccupationType);
         for(int i= 0; i<9; i++)
         {
             occupationTypeOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='occupationTypeDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();

        // Select Occupation Industry.
         driver.findElement(By.xpath(".//*[@id='occupationIndustryDemographicTabBtnTd']/a")).click();
         WebElement selectOccupationIndustry = driver.findElement(By.id("categoryForm:demographics_avail_occupationIndustry"));
         Select occupationIndustryOptions = new Select(selectOccupationIndustry);
         for(int i= 0; i<13; i++)
         {
             occupationIndustryOptions.selectByIndex(i);

         }
         driver.findElement(By.xpath(".//*[@id='occupationIndustryDemographicDiv']/table/tbody/tr[3]/td[2]/input[1]")).click();



     }
     /*@Test
      public void logout()
      {
          driver.findElement(By.xpath(".//*[@id='dd_Icon']")).click();
          driver.findElement(By.xpath(".//*[@id='homeForm:top:lnkLogoutTxt']")).click();
      }

      @AfterTest
      public void closeBrowser()
      {
          driver.quit();
      }*/
}

1 个答案:

答案 0 :(得分:0)

这些是您代码中唯一相关的部分:

public class CreateSites 
{
    public WebDriver driver = new FirefoxDriver();

    @BeforeTest
    public void setup(String browserName) throws Exception {
            //create firefox instance
            driver = new FirefoxDriver();
     }

    /*
    @AfterTest
    public void closeBrowser()
    {
        driver.quit();
    }*/
}

其他一切与您的问题无关。

班级new FirefoxDriver()的第一行打开Firefox浏览器。在每次测试之前,再次调用new FirefoxDriver()打开另一个浏览器。最后,您注释掉driver.quit(),这通常会关闭浏览器!

你可能想要的是:

public class CreateSites 
{
    public WebDriver driver;  // do not open Firefox here!

    @BeforeTest
    public void setup(String browserName) throws Exception {
            //create firefox instance
            driver = new FirefoxDriver();
     }

    @AfterTest  // make sure this runs!
    public void closeBrowser()
    {
        driver.quit();
    }
}