如何在Chrome Devtool控制台上触发点击事件? [英] How to trigger a tap event on Chrome devtool console?
问题描述
如何使用Chrome-devtool的控制台测试我的JavaScript是否有效?我已经找到xpath并将其转换为css定位器。基本上这是一个将颜色从灰色转换为blye的按钮。
How can I use the Chrome-devtool's console to test if my javascript works? I've located the xpath and converted it to an css locator. Basically it is a button that turns the color from grey to blye.
这是我的代码片段:
browser.execute_script($('button.nominate ').trigger('tap');)
Here is my snippet code: browser.execute_script("$('button.nominate').trigger('tap');")
在控制台上,我尝试过如下:
On the console, I tried something like:
$('button.nominate')。trigger('tap')
$('button.nominate').trigger('tap')
下面显示的结果:
[]
[]
它会点击按钮
推荐答案
我想你正在对您的手机应用程序进行某种功能测试。前一段时间我正在做同样的事情(使用CasperJS),在此过程中,我创建了这个功能:
I suppose you are doing some kind of functional testing on your mobile app. I was doing the same thing some time ago (using CasperJS) and, in the process, I've created this function:
// I've commented out CasperJS specific stuff, don't use it if you don't need it
function triggerEventOnPage(selector, eventName, memo) {
//casper.evaluate(function(selector, eventName, memo){
var event;
var element = document.querySelector(selector);
event = document.createEvent("Event");
event.initEvent(eventName, true, true);
event.memo = memo || { };
element.dispatchEvent(event);
//}, selector, eventName, memo);
//wait();
}
您可以通过调用以下方式在您的测试中使用它:
You can use it in your tests by calling:
triggerEventOnPage(".edit-list-button", 'tap');
但是,请注意,没有本机点击
事件。只有 touchstart
, tachmove
, touchend
事件和实现的点击
是基于这三个。因此,您使用的点击
事件的执行可能与我使用的不同,上述功能可能无法为您工作。
However, mind that there is no native tap
event. There are only touchstart
, tachmove
, touchend
events and implementation of tap
is done based on those three. Therefore, implementation of tap
event that you are using may differ from one that I was using and the function above may not work for you.
编辑:触发('tap')应该工作很好。 @NULL可能是选择器无效。
since you are using jQuery, $('button.nominate').trigger('tap')
should work just fine to. @NULL may be right that your selector is invalid.
这篇关于如何在Chrome Devtool控制台上触发点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!