The Selenium ‘click’ command
Sauce AI for Test Authoring: Move from intent to executing in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to executing in minutes.
|
x
Blog
The Selenium ‘ click ’ dictation
Learn how the & quot; Selenium.click () ” command work to simulate exploiter pawl.
The golden normal of web application testing states that & quot; You can find a act of bugs by only clicking randomly on various places. & quot; This is especially true for User Interface bug. If you are using Selenium or Sauce Connect for automating your application ’ s User Interface tryout, it is important to cognize how the “ Selenium.click () ” dictation works in order to simulate user clicks.
The “ Selenium.click () ” command is a lesser cognize, yet powerful capability for testing the doings of app UI elements, without manual intervention. For instance, we recently tested a JSP form with a few dozen dropdown list (individual and multi-select), checkboxes, and a plethora of radio buttons. Clicking each UI control manually would have been a pain in the script. In contrast, simulating these clicks using Selenium not solely saved crucial manual testing effort, it helped uncover a bit of important bugs in the application as well.
How to use the Selenium Click command
To put it in bare words, the clink command emulate a click operation for a link, button, checkbox or radio push. In Selenium Webdriver, execute click after finding an element. In SeleniumIDE, the recorder will do the identifying, and the dictation is simply click.
Example: The following bid invocation simulates a click on a button element call myButton
element = driver.find_element: xpath, & # x27; //input [@ name= & quot; q & quot;] & # x27;
element.click ();
Advanced Click Techniques
For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.
Previous versions of Selenium had a driver-level .click () event that could click any object on the screen by an identifier. These identifier include css=, name=, id=, link= and xpath=. Today, that functionality only live in the IDE. The IDE also has the power to set wherein an object you click, in terms of offset from top-left, using the “ click at ” bid.
Rotating through chatter aim can be extremely powerful. Here is a mere example that clicks target in linear order. You could observe the element as they are bestow, write code to do the check, or, with a small work, randomize the clicking.
The HTML is bare plenty. It is a web page with ten push that adds a click number after each click:
& lt; HTML & gt;
& lt; TITLE & gt; TEST SCREEN & lt; /TITLE & gt;
& lt; script & gt;
var i = 1;
function printme () {
doc = document.getElementById (& quot; printmediv & quot;);
txt = document.createTextNode (& quot; Clicknumber & quot; + i + & quot; & quot;);
doc.appendChild (txt);
doc.appendChild (document.createElement (& quot; br & quot;));
i=i+1;
}
& lt; /script & gt;
& lt; BODY & gt;
& lt; FORM & gt;
& lt; H2 ID= & quot; First & quot; & gt; Sample Text & lt; /H2 & gt;
& lt; BR/ & gt;
This is a exam
& lt; Br/ & gt;
& lt; div & gt; Click hither friend
& lt; input type= & quot; button & quot; value= & quot; yoyoyo & quot; id= & quot; yoyoyo & quot; / & gt;
& lt; BR/ & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit1 & quot; id= & quot; submitter1 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit2 & quot; id= & quot; submitter2 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit3 & quot; id= & quot; submitter3 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit4 & quot; id= & quot; submitter4 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit5 & quot; id= & quot; submitter5 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit6 & quot; id= & quot; submitter6 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit7 & quot; id= & quot; submitter7 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit8 & quot; id= & quot; submitter8 & quot; onclick= & quot; printme (); homecoming false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit9 & quot; id= & quot; submitter9 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; input type= & quot; submit & quot; value= & quot; Submit10 & quot; id= & quot; submitter10 & quot; onclick= & quot; printme (); return false; & quot; / & gt;
& lt; BR/ & gt;
& lt; BR/ & gt;
& lt; /div & gt;
& lt; H2 & gt; Simple Text & lt; /H2 & gt;
& lt; div id= & quot; printmediv & quot; & gt; & lt; /div & gt;
& lt; /FORM & gt;
& lt; BODY & gt;
& lt; /HTML & gt;
The HTML isavailable in Githubfor your reexamination.
The interesting bit, of course, is the codification to do the clicking.
Ruby Loop Through The Clicks
The code below (and it Github) loop through submit buttons, clicking them. Code to check that the text is created, or to do different textbook for different buttons, or to click in random order, is an exercise for the reader. Fork our git and connectedness to your work in the comments to build your reputation!
require & quot; selenium-webdriver & quot;
require & # x27; test/unit & # x27;
class SampleTest & lt; Test: :Unit: :TestCase
def setup
selection = Selenium: :WebDriver: :Chrome: :Options.new
options.add_argument (& # x27; -- ignore-certificate-errors & # x27;)
options.add_argument (& # x27; -- disable-popup-blocking & # x27;)
options.add_argument (& # x27; -- disable-translate & # x27;) & lt;
@ driver = Selenium: :WebDriver.for: chrome, options: options
end
def teardown
@ driver.quit
end
def test_click_button
# Only prove on mac - finds sample.html in the current working directory
directory = File.expand_path File.dirname (__FILE__)
specific_filename = & quot; file: // & quot; + directory + & quot; /sample.html & quot;
@ driver.navigate.to specific_filename
for i in 1 .. 10 do
id = & quot; submitter & quot; +i.to_s ();
css = & quot; input [id= & # x27; & quot; + id + & quot; & # x27;] & quot;;
put & quot; snap & quot; + id;
element = @ driver.find_element: css, css;
element.click ()
sleep(1);
end
# If we get this far we are okay
assert (true);
end
end
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free