WebDriver / TestNG:浏览器启动两次

时间:2014-04-08 13:09:36

标签: selenium-webdriver

我是Webdriver的新手。我在具有代理设置的firefox浏览器中为我的应用程序实现了一个datadriven测试。不知怎的,我的浏览器在执行时启动了两次。有人可以帮我解决出错的地方

这是我的代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import jxl.*;
import jxl.read.biff.BiffException;

public class Datadriven_login {

    public static WebDriver ff =  new FirefoxDriver();


  @Test
  public void f() throws BiffException, IOException, Throwable {

      FileInputStream file = new FileInputStream ("C:\\Users\\user\\Desktop\\Datadriven.xls");
      Workbook w = Workbook.getWorkbook(file);
      Sheet s = w.getSheet(0);
      for(int rows =1 ; rows <= s.getRows(); rows++)

      {
          FirefoxProfile ffp = new FirefoxProfile();
          ffp.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
          ff = new FirefoxDriver(ffp);
          ff.get("https://xyz.do");

          String username = s.getCell(0, rows).getContents();

          ff.findElement(By.xpath("//*[@id='TableMain']/tbody/tr/td/form/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr[1]/td[2]/input")).sendKeys(username);
          String password = s.getCell(1, rows).getContents();
          System.out.println( rows + "-" +  "-" + username + "/"+ password);
          ff.findElement(By.xpath("//*[@id='TableMain']/tbody/tr/td/form/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/input")).sendKeys(password);

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

每当您实例化一个新的WebDriver时,都会打开一个新的浏览器。

因此,这一行:

public static WebDriver ff = new FirefoxDriver();

和这一行:

ff = new FirefoxDriver(ffp)

都实例化它。我建议将第一行更改为:

public static WebDriver ff;

答案 1 :(得分:0)

你已经两次实例化了FirfoxDriver。

替换

  

public static WebDriver ff = new FirefoxDriver();

  

public static WebDriver ff;