2014-04-08 69 views
1

我是Webdriver的新手。我使用代理設置在Firefox瀏覽器中爲我的應用程序實施數據驅動測試。不知怎的,我的瀏覽器在執行時啓動了兩次。是否有人可以幫助我在什麼地方出了錯WebDriver/TestNG:瀏覽器啓動兩次

這裏是我的代碼:

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); 

感謝您的幫助!

回答

1

每當你實例化一個新的WebDriver,一個新的瀏覽器將打開。

因此,這一行:

public static WebDriver ff = new FirefoxDriver();

和這一行:

ff = new FirefoxDriver(ffp)

兩個實例化。我會建議改變的第一行:

public static WebDriver ff;

0

你實例化FirfoxDriver兩次。

替換

公共靜態的webdriver FF =新FirefoxDriver();

公共靜態的webdriver FF;