2016-06-24 48 views
1

我嘗試在我的Selenium測試中使用PhantomJS驅動程序,但我沒有成功恢復我的Firefox配置文件以避免我在網站中登錄。 這是我的代碼:無法在Selenium中使用PhantomJS驅動程序和Firefox配置文件

import static org.junit.Assert.fail; 

import java.util.concurrent.TimeUnit; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxProfile; 
import org.openqa.selenium.firefox.internal.ProfilesIni; 
import org.openqa.selenium.phantomjs.PhantomJSDriver; 
import org.openqa.selenium.remote.DesiredCapabilities; 

public class Stackoverflow { 
    private WebDriver driver; 
    private String baseUrl; 
    private StringBuffer verificationErrors = new StringBuffer(); 

    @Before 
    public void setUp() throws Exception { 
     System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "/lib/phantomjs-2.1.1-windows/bin/phantomjs.exe"); 
     DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); 
     driver = new PhantomJSDriver(capabilities); 
     driver.manage().window().maximize(); 
     baseUrl = "http://stackoverflow.com/"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testStackoverflow() throws Exception { 
     driver.get(baseUrl); 
    } 

    @After 
    public void tearDown() throws Exception { 
     driver.quit(); 
     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      fail(verificationErrorString); 
     } 
    } 
} 

你能告訴我怎麼設置PhantomJS驅動程序?

回答

0

您無法使用PhantomJS的firefox配置文件,因爲您嘗試使用帶有PhantomJS的firefox配置文件... PhantomJS不是firefox,您認爲這樣做是如何工作的。

+0

我知道這是不可能的,當然,但我問是否有使用Firefox或其他的解決方案。 – Steefler35

0

使用此代碼來設置phantomjs驅動程序的路徑,使用這個,讓我知道,如果你面對的任何問題:

File file = new File("E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");    
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());   

OR

System.setProperty("phantomjs.binary.path", "E:\\software and tools\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");  
WebDriver driver = new PhantomJSDriver(); 
相關問題