在本文中,您将会了解到关于使用WebDriver时是否可以忽略JavaScript异常的新资讯,同时我们还将为您解释HtmlUnit,Ruby绑定的相关在本文中,我们将带你探索使用WebDriver时
在本文中,您将会了解到关于使用WebDriver时是否可以忽略JavaScript异常的新资讯,同时我们还将为您解释HtmlUnit,Ruby绑定的相关在本文中,我们将带你探索使用WebDriver时是否可以忽略JavaScript异常的奥秘,分析HtmlUnit,Ruby绑定的特点,并给出一些关于HtmlUnit可以处理JavaScript重定向吗?、HtmlUnit忽略JavaScript错误?、javascript – Selenium WebDriver点击隐藏元素、javascript – Webdriverio黄瓜不能使用承诺的实用技巧。
本文目录一览:- 使用WebDriver时是否可以忽略JavaScript异常(HtmlUnit,Ruby绑定)(webdriver用法)
- HtmlUnit可以处理JavaScript重定向吗?
- HtmlUnit忽略JavaScript错误?
- javascript – Selenium WebDriver点击隐藏元素
- javascript – Webdriverio黄瓜不能使用承诺
使用WebDriver时是否可以忽略JavaScript异常(HtmlUnit,Ruby绑定)(webdriver用法)
当我加载页面时,HtmlUnit引发异常并使测试崩溃
caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)driver.navigate.то url
ReferenceError:未定义“
x”。(net.sourceforge.htmlunit.corejs.javascript.EcmaError)
如果我使用Firefox驱动程序,则不会引发任何异常。
caps = Selenium::WebDriver::Remote::Capabilities.firefox
或为HtmlUnit驱动程序禁用JavaScript
caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => false)
我无法更改测试页上的代码并解决问题,因此我需要忽略它,或者以任何方式使用Firefox JavaScript Engine而不是标准的HtmlUnit
JavaScript Engine。
是否可以在不更改测试页代码的情况下解决我的问题?
更新: 尝试将Capybara + WebKit替换为Selenium + HtmlUnit的替代方法-
正常运行,没有错误。但是我仍然想在不更改框架的情况下解决问题。
答案1
小编典典查看的来源后HtmlUnitDriver
,似乎无法自定义要更改的行为。解决此问题最简单的方法是修补并重新编译Selenium服务器(可能是,也可能不是)。您需要添加以下行:
--- HtmlUnitDriver.java 2012-01-05 17:45:22.779579136 +0100+++ HtmlUnitDriver.java 2012-01-05 18:14:51.415106195 +0100@@ -255,6 +255,7 @@ WebClient client = newWebClient(version); client.setHomePage(WebClient.URL_ABOUT_BLANK.toString()); client.setThrowExceptionOnFailingStatusCode(false);+ client.setThrowExceptionOnScriptError(false); client.setPrintContentOnFailingStatusCode(false); client.setJavaScriptEnabled(enableJavascript); client.setRedirectEnabled(true);
HtmlUnit可以处理JavaScript重定向吗?
//上下文:
如果连续有5个JavaScript重定向,我只能看到它停止的页面的URL – 我无法查看它运行的5个URL.
也许我可以将记录器连接到回调方法以查找重定向?不确定这是否可行,或者它是如何工作的..
解决方法
HtmlUnit忽略JavaScript错误?
EcmaError: lineNumber=[671] column=[0] linesource=[null] name=[TypeError] sourceName=[https://reservations.besodelsolresort.com/asp/CalendarPopup.js] message=[TypeError: Cannot read property "parentNode" from undefined (https://reservations.besodelsolresort.com/asp/CalendarPopup.js#671)] com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "parentNode" from undefined (https://reservations.besodelsolresort.com/asp/CalendarPopup.js#671)
有没有反正我可以忽略这个错误?我不是特别关心日历是否正确加载.
解决方法
WebClient client = new WebClient(); client.getoptions().setThrowExceptionOnScriptError(false);
javascript – Selenium WebDriver点击隐藏元素
我知道用硒1我可以这样做如下:
selenium.click(id="idOfHiddenField");
并且这将工作,但是与硒2(WebDriver),这不是.我不想使用jquery来启用或显示隐藏的字段或JavaScript.这是因为大多数测试都在使用xpath.
或者我只需要留下旧的硒,让你点击隐藏的领域?
解决方法
例如:
document.getElementsByClassName('post-tag')[0].click();
上述JavaScript将点击此页面右上方的“Selenium”标签(旁边的问题),即使它被隐藏(假设).
所有你需要做的是通过JavascriptExecutor接口发出这个JS指令,如下所示:
(JavascriptExecutor(webdriver)).executeScript("document.getElementsByClassName('post-tag')[0].click();");
这将使用JS沙箱和综合点击事件来执行点击操作.虽然它破坏了WebDriver用户活动模拟的目的,但是您可以将其用于您的情况下,在您的情况下很好的效果.
javascript – Webdriverio黄瓜不能使用承诺
实际上,我想介绍这个简单的功能:
Feature: Example Feature In order to become productive As a test automation engineer I want to understand the basics of cucumber Scenario: My First Test Scenario Given I have open "https://google.com" Then the title should be "Google". And the bar should be empty.
通过这个测试:
const assert = require('assert'); module.exports = function() { this.Given(/^I have open "([^"]*)"$/,function(arg1,callback) { browser .url(arg1) .call(callback); }); this.Then(/^the title should be "([^"]*)"\.$/,callback) { // First solution const title = browser.getTitle(); assert(title,arg1); // Second solution browser .getTitle() .then(title2 => { assert(title2,arg1); callback(); }); }); this.Then(/^the bar should be empty\.$/,function(callback) { // Write code here that turns the phrase above into concrete actions callback(null,'pending'); }); }
我的配置文件:
"use strict"; const WebDriverIO = require('webdriverio'); const browser = WebDriverIO.remote({ baseUrl: 'https://google.com',// Or other url,e.g. localhost:3000 host: 'localhost',// Or any other IP for Selenium Standalone port: 4444,waitforTimeout: 120 * 1000,logLevel: 'silent',screenshotPath: `${__dirname}/documentation/screenshots/`,desiredCapabilities: { browserName: process.env.SELENIUM_broWSER || 'chrome',},}); global.browser = browser; module.exports = function() { this.registerHandler('BeforeFeatures',function(event,done) { browser.init().call(done); }); this.registerHandler('AfterFeatures',done) { browser.end().call(done); }); };
我的问题
我的问题是:
>我从未传入.call(回调)函数
>如果我通过在.url(arg1)之后添加callback()来绕过前一点,我会转到下一个点
>在第一个然后,第一个解决方案和第二个解决方案似乎都不起作用.当我试图记录const标题值时,我有一个未决的承诺.但是,当我试图解决这个承诺(第二种情况)时,我从不记录任何事情(即使在拒绝的情况下).
约束
>我不想使用wdio
>我使用的是硒2.53
>我使用的是cucumberjs 1.3.1
>我使用的是webdriverio 4.4.0
>我使用的是Nodejs v4.6.0
编辑:我总是有超时问题
解决方法
今天关于使用WebDriver时是否可以忽略JavaScript异常和HtmlUnit,Ruby绑定的分享就到这里,希望大家有所收获,若想了解更多关于HtmlUnit可以处理JavaScript重定向吗?、HtmlUnit忽略JavaScript错误?、javascript – Selenium WebDriver点击隐藏元素、javascript – Webdriverio黄瓜不能使用承诺等相关知识,可以在本站进行查询。
本文标签: