Selenium WebElement.click() vs. Javascript click event - java

I was wondering what the differences are between calling the click() method of the WebElement versus finding the element by id and firing the click event with JavaScript.
Just to be clear in the first method I call the .click() of an instance of WebElement:
myWebElement.click();
The second technique is:
((JavascriptExecutor)driver).executeScript("document.getElementById('myElementID').click()");
I'm interested in knowing all the differences between these two techniques for clicking web elements, and also advantages and disadvantages of each.

Webdriver utilizes a browser's native support for mapping the DOM element to WebElement object using id/xpath etc.
The JavascriptExecutor.executeScript executes an external script in the context of the currently selected browser window. (similar to an augmented browsing tool like grease monkey, if you ever used),
and in case the script returns any DOM element its converted into WebElement object.
One can also say, the click simulated by WebDriver on a browser is similar to what actual user do as compared to one invoked using javascript.
In reality, with WebDriver not all the events can be automated flawlessly with all the web browsers, in fact with different versions of the same Web browser also. (i.e. different version of IE, FF etc behave differently). Still WebDriver is the near best tool available for this.
Once (~4 years back) on a certain version of IE we observed that we can't send right click or may be hover mouse on generated menu links, so we used js to simulate that, which performed very much browser independent way. so you can now conclude what executing external javascript can be good for.
Also, there are automated web testing frameworks which use javascript for everything instead of browser's native support. e.g. :http://en.wikipedia.org/wiki/Sahi_%28software%29
Ref:
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeScript%28java.lang.String,%20java.lang.Object...%29
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#findElement%28org.openqa.selenium.By%29

Those kind of tests are E2E (end to end) not BDD.
First one – is executed now, to take next action you must write some function that will delay execution for e.g download new data from server.
The second code return promise – http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_WebElement.html – „Schedules a command to click on this element.” – you can use then callback to run next action.

Related

Website Scraping in Java once the whole site is loaded

In few websites few scripts might take some time to run which results the website scraping to work inefficiently or the html which is returned from the scraper is incomplete.How to scrape the website once the site scripts are fully ran.
I am using URL Connection in java when I am reading the text from it I am getting HTML which is pre matured (i.e) I have script which is a bit long which takes some time to load which changes color of the text which is not reflecting in the text which is read using URL CONNECTION.
You can use PhantomJS. It's a browser but headless. It will render all js on the page. You might find this thread useful Any Java equivalent to PhantomJS?
I have used Selenium in java (and kotlin using the java libarary) to do website automation and testing
it can be set up to wait a specified time before looking for elements or wait until it is loaded, since it really just remote controls a webbrowser you can use javascript on pages and act just like any user would
https://www.seleniumhq.org/download/
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
RemoteWebDriver driver = new ChromeDriver()
driver.get(url)
driver.findElement(by.name("search")).sendKeys("some query")
driver.find(by.id("submit")).click()
you can wait for all things to load as described here
https://stackoverflow.com/a/33349203/9006779
(or at least in a similar way, the api might have changed)

Trigger an event from chrome developer studio

I want to trigger an event directly from the Chrome developer tools.
I know that I can inspect an element and than reference to it using $0 in the console.
But how can I trigger a dom-event, on this element?
Yes you can. As a matter of fact you can try it right here on Stackoverflow.
Inspect your inbox. You will trigger a click event on that element.
Stackoverflow is convenient since it has jQuery already loaded so you can just do
$($0).trigger( "click");
And you will see the inbox behave as you would expect (open).
The command above uses jQuery $() to wrap the element $0 you have available.
With this you have all the beautiful functionality of jQuery.
In general you can use vanilla javascript for this too, but the commands for triggering events depend on the browser you are using.
You can of course load jQuery yourself via the command line
just copy the content of this paste it in the command line and press enter.
This will work great if the page you are viewing doesn't do anything that would cause any conflicts.
Moreover you can read this answer for vanilla javascript.
Considering that you're using chrome, you can use the URL-bar to simply type
javascript:document.getElementById("submit-button").form.submit()
to, in this case, submit an answer to StackOverflow. This way you can also trigger any other event, for example a url-click:
javascript:document.getElementById("answer-33569491").children[0].children[0].children[0].children[0].children[0].children[1].click()
This can also be entered in a watch, then you can execute it multiple times.

Selenium is breaking my page in IE11 when I set text

I made some Automation tests with Selenium working on chrome, but now when I tried them in IE11 I got this weird behaviour: I land in a Page with a Search Box and a Search Button, pretty standard, and when I either set(" ") or sendKeys(" ") to an input textbox, the page "moves" to the left as is it had broken, imagine the page in the center of the screen and suddenly the left margin would dissapear and the page would stretch to the left... Does anybody know what I'm talking about?
Any help please?
BTW I also tried doing some actions in other elements and they won't break the page, also clicking the textbox won't do the trick, it happens when I put text in it
Well, "sendKeys" is likely what we call a "native event" and so it depends highly on the implementation of the binary driver for IE. For example, Selenium only officially supports native events on Firefox up to version 31.0.6 and not versions 32+. IE11 is pretty new and also comes in different versions (since it auto-updates). So, if you need to do a sendKeys that is non-native (which is usually not necessary) then you can probably code one using a JavascriptExecutor object. Let us know if that works for you.
-- addendum --
The Selenium team would tell you that they didn't want to include the javascriptExecutor funtionality in Selenium, but it is scenarios like this where the executor capability shows its true value.

Click on hidden elements with Selenium java WebDriver, without using the javascript executor?

Is it possible to click on hidden/non-visible elements, using the selenium java webdriver, without using the JavaScript executor to click via JavaScript? The tests I'm performing are in a browser with JavaScript disabled, which is why I can't use the jse.
Selenium is meant to replicate the end user's behavior. Since any end user cannot do anything with the hidden element it's not realistic to interact with hidden elements directly. See this answer.
So, the answer is No there is no way without the javascript executor. Selenium does not interact with the hidden element directly

How to select proper Action for web target in Selenium IDE

I am new to Selenium IDE. As far as I know, when open Selenium IDE, you will notice that the red 'record macro' button is toggled. This means that selenium will attempt to record every action you make inside the browser. This is a problematic way of recording as we implicitly wait for actions to complete before moving on.
If I only let Selenium to record every actions without specifying extra actions, many test step will be failed with error message : Element not found. I was trying to add extra actions based on Selenium API, like waitForElementPresent, waitForSearch etc.
My question is: How do I know which extra action do I need to add for each web target? Any standard for it? Thanks!
I use webdriver but I am familiar with IDE and so far I know it depends on your application how you want to handle the tests. If your application uses ajax calls you might need to use some frequent waitForElementPresent or waitForSearch etc.. and Assertions also depend on the needs of your tests.
Now, the question is how do you know which extra step do you need to insert?
Ans. is you will know the necessity. Such as, if your test step depends on a previous ajax call to finish then you know there is a wait necessary and you know what to do. Not to mention, you can always insert extra steps and I am sure you already know that. And, there is no standard for using those. You adjust your tests depending on your necessity
you need to go through introduction to selenium ide or just think straight this way that if any action needs loading of page, you simple need to wait for element present and the perform click
click|target|
waitForElementPresent|target|
or if you need to store any value you can use
storeEval|target|value
also the variable name in the selenium ide is named followed by $variableName
enter can be performed as ${KEY_ENTER}
to verify any value we can use AssertValue or VerifyValue
the difference between assert and verify is that assert stops the execution of test case if the value is false whereas verify gives error and execute next statement.
these are few points to be noted in selenium ide.
hope this answer would help you!
You may want to try Implicit Wait addon for Selenium IDE. It will automatically call WaitForElementPresent before executing actions on that element (like clicks). This may save you some time.
Here is the link of Selenium API, all actions can be found here.

Categories