简体   繁体   English

是否可以在Watir-WebDriver中显示PhantomJS?

[英]Is it possible to make PhantomJS visible in Watir-WebDriver?

我知道可以将phantomJS与Watir-Webdriver一起使用,但是在我决定使用(默认)无头模式之前,是否有一些'模式'可以传递它以实际看到它在做什么?

Not sure about a mode, but you can try this: 不确定模式,但你可以试试这个:

require 'watir-webdriver'
b = Watir::Browser.new :phantomjs
b.screenshot.save 'step1.png'
b.goto "www.google.com"
b.screenshot.save 'step2.png'
b.url #"http://www.google.com/"
b.screenshot.save 'step3.png'
b.title #"Google"
b.screenshot.save 'step4.png'

You'll get output that looks like this: 您将获得如下所示的输出: 第2步的屏幕截图

Extending other responses, you can automatically do screenshots before clicking or sending keys to element. 扩展其他响应,您可以在单击或向元素发送键之前自动执行屏幕截图。

class Screenshotter < Selenium::WebDriver::Support::AbstractEventListener
  def before_click(_, driver)
    driver.save_screenshot("screenshot-#{Time.now.to_i}.png")
  end

  def before_change_value_of(_, driver)
    driver.save_screenshot("screenshot-#{Time.now.to_i}.png")
  end
end

browser = Watir::Browser.new(:phantomjs, listener: Screenshotter.new)

Moving forward, you can join all screenshots to a GIF which will work like live video of your test. 接下来,您可以将所有屏幕截图加入到GIF中,这将像测试的实时视频一样工作。

Like artjom B is writing, there is not possibility for other modes than headless in PhantomJS. 就像artjom B正在写作一样,PhantomJS中除了无头之外其他模式是不可能的。

So either change the browser - I have made all my tests able to run in every browser by changing an Environment variable in the CMD box. 因此要么改变浏览器 - 我通过更改CMD框中的环境变量,使我的所有测试都能够在每个浏览器中运行。

Or else take a lot of screenshots with: @b.screenshot.save 'filename' - and you can follow the flow through even though you keep on using PhantomJS 或者使用以下内容拍摄大量屏幕截图:@ b.screenshot.save'filename' - 即使您继续使用PhantomJS,也可以按照流程进行操作

You could log all the request and responses using onResourceRequested and onResourceReceived api's. 您可以使用onResourceRequestedonResourceReceived api记录所有请求和响应。 I do this for a scraper I wrote in casperjs which uses phantomjs underneath. 我这样做的是我在casperjs中写的一个刮刀,它使用下面的phantomjs。 The Watir-Webdriver should provide some hook into webPage object of phantomjs. Watir-Webdriver应该为phantomjs的webPage对象提供一些钩子。

page.onResourceRequested = function(requestData, networkRequest) {
  console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};

page.onResourceReceived = function(response) {
  console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM