Friday, 15 January 2010

selenium webdriver - How do we correctly click on a link or button using Javascript? -



selenium webdriver - How do we correctly click on a link or button using Javascript? -

i'm using php + ajax version of template: http://192.241.236.31/themes/preview/smartadmin/1.5/ajaxversion/, codeception webdriver (selenium) perform tests. of them working fine, have random, yes, random!, failing tests, them , seek harden, hoping not randomly fail sometime in future. 1 of failing reasons due wrong clicks on interface. tell codeception click #element-id , when fails, see had different page showing in output png, link activated showing tried click right link. have load different page before clicking particular link , wait 10 seconds render, , works, silly huh? yeah, doesn't work either.

this how used test things:

$i->click('#element-id');

and have utilize element id, because it's multi-language app, (mostly) strings come strings file , might alter @ point , break tests.

after many random tests failing, decided create frontend responsible clicking things during tests, it's long , silly circunventing shot, should work, created functionalhelper:

public function clickelement($element) { $i = $this->getdriver(); $i->executejs("clickelement('{$element}');"); }

and 2 javascript functions:

function clickelement(element) { element = jquery(element); if(typeof element !== undefined) { fakeclick(element[0]); } homecoming true; } function fakeclick(object) { if (object.click) { object.click(); } else if(document.createevent) { var evt = document.createevent("mouseevents"); evt.initmouseevent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); var allowdefault = object.dispatchevent(evt); } }

i'm using jquery in first 1 because it's available in template , it's easier select non-id'ed things.

this working fine, , can test by

1) opening page:

http://192.241.236.31/themes/preview/smartadmin/1.5/ajaxversion/

2) loading script in browser console executing

loadscript('https://www.dropbox.com/s/kuh22fjjsa8zq0i/app.js?dl=1');

3) , clicking things:

calendar

clickelement($x('//*[@id="left-panel"]/nav/ul/li[7]/a/span'));

widgets

clickelement($x('//*[@id="left-panel"]/nav/ul/li[8]/a/span'));

because template using hash character command ajax part of page , not reload everything, had (programatically) add together referer-url href links, could, in app, redirect referer, hash part uf accessed url not sent server. link example:

<a href="/users?referer-href-url=/dashboard" title="users" id="users-navigation"> <span class="menu-item-parent">users</span> </a>

wich means user looking @ dashboard when clicked users link. if wrong happens, app redirect user dashboard, error message.

the problem that, somehow, during tests, current url, when tested using:

$i->seecurrenturlequals('/#/clients?referer-href-url=/clients');

becomes

/#/clients

instead of

/#/clients?referer-href-url=/clients

this happens sometimes, because other works. if browse site manually works in 100% of time , see right url in address bar. , if manually execute clickelement() works fine. problem heppens my suite running.

here's illustration of passing (green):

and exact same test randomly failing (red):

this code related failing test:

$i->clickelement('#clients-navigation'); $i->waitfortext('jane doe', 10); $i->seecurrenturlequals('/#/clients?referer-href-url=/clients');

i wait after click, page can have time render.

you can see there lot of "i click element", using php , javascript functions without problem.

i'm using:

ubuntu 14.04 php 5.5.9 codeception 2.0.7 selenium 2.44.0 firefox 33.0

so, problem here? right way click element in javascript?

in process experience tests not fail when ran singly , fails when in batch, might different question, right?

javascript selenium-webdriver codeception

No comments:

Post a Comment