2016-06-24 49 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(); 
相关问题