2017-08-01 273 views
0

使用Cucumber和Selenium测试网站。在我的hooks.js文件中,我有以下内容:Selenium Webdriver Black屏幕截图PhantomJS

driver.get("https://localhost:8000/"); 

sleep(2000); 

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver); 

var btn = this.driver.wait(selenium.until.elementLocated(By.css('#app > div > div > div.col-xs-6.textColumn > button'), seleniumTimeOut)); 

TakeScreenshot('./test_artifacts/img/', 'Load Success', driver); 

this.driver.sleep(3000); 

这里的目标是成功加载页面并截取它。该网站正在运行本地主机。发生截图时会出现问题。无论我有多长时间让司机入睡,我都会得到一张黑色的截图,表明我的网站没有及时“建立”(根据情况使用可能不正确的术语)。如果我更改URL,以https://google.com/我得到了现场,没有任何问题的屏幕截图

Waiting for element to be located By(css selector, #app > div > div > div.col-xs-6.textColumn > button) 
Wait timed out after 20112ms 

:然后我得到这个错误。有什么想法发生在这里?我的上述假设是否正确? 在此先感谢!

回答

0

这个问题似乎已经认证的问题。测试将在http上运行,但当localhost使用https时,测试将失败。 我的解决方法是在我的hooks.js文件中添加以下内容到this.BeforeFreature中:

this.driver = new selenium.Builder() 
     .withCapabilities({'phantomjs.cli.args': ['--ignore-ssl-errors=true']}) 
     .forBrowser('phantomjs') 
     .build(); 
0

请尝试等待其他一些元素可用。使用xpath定位器代替CSS。你的机器是否代理?如果意味着请在代理后面添加幻像浏览器。

var phantom = require('phantom'); 
phantom.create(function(browser){ 
    browser.createPage(function(page){ 

    browser.setProxy('98.239.198.83','21320','http', null, null, function(){ 

    page.open(
     'http://example.com/req.php', function() { 

     });});});}); 
0

首先,将驱动程序更改为chrome驱动程序,并在更改URL后查看脚本正在执行的操作。不同的环境有一段时间会有不同的id和XPath影响脚本。因此,在直接转到phantomjs之前,请先使用chrome或firefox等常见驱动程序检查脚本的行为。

我已经经历了相同的情况,我也经历过相同的情况。这是一个愚蠢的错误这是浪费了我一半的一天:P

希望它会帮助你:)