On which Thread JxBrowser execute click DOM element - java

Here we have a simple DOMElement
DOMElement button = document.findElement(By.id("action_button");
... and when I execute click on them as
button.click();
And I have a question: Did API will execute click on the same Thread ? Or will be created other Thread which execute this .click() ?
I ask about it because I search best way (the fastest way) to execute click at DOM elements by browser.

When you invoke the click() method it adds the click message into the message queue which is processed in the separate thread asynchronously by message sender.

Related

Does Selenium WebElement.Click() wait until the next page is loaded?

I tell Selenium to wait until it sees an element - Selenium sees it
I tell Selenium to click on this element, it is a button to link to a new page - Selenium click on it.
The problem is that after clicking it, Selenium will then wait until the next page is fully loaded (the page sometimes loads in a second, or waits for ages, I think it's a problem with Zen Desk Live Chat on that page).
When it is fully loaded it will then throw an error and say that the element it clicked on cannot be seen (naturally it can't because it is now on a new page)
I have tried changing the wait time with
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
however this doesn't help.
I have also tried
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
However this has the same problem.
Below is the code I am currently using.
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath(element)));
WebElement we = driver.findElement(By.xpath(element));
we.click();
System.out.println("Clicked: " + element);
}catch (Exception e){
System.out.println(e.getMessage());
}
I expect that once the element has been clicked on, that Selenium just carries on without caring if the next page has loaded up or not.
However what happens is when the 2nd page loads up, sometimes the page gets stuck "waiting for widget-mediator.zopim.com" and Selenium will not progress past the click() line until the the WebDriverWait time has expired (60 seconds)
After the 60 seconds has expired I see this error in the Console Output:
[1561374309.111][SEVERE]: Timed out receiving message from renderer: 60.000
[1561374309.112][SEVERE]: Timed out receiving message from renderer: -0.002
Is something else happening here? Or does Click() wait until the page has loaded, if that click takes it to a new page? If it does is there a way to tell it to stop waiting? I have other code to check if the page has loaded or not, but I don't need Click() to do this.
Thanks in advance.
Selenium’s (or more correctly, WebDriver’s) behavior on click is governed by the W3C WebDriver Specification. In that document, the full algorithm is defined for what happens when an element click is requested. In general, if the click will navigate to a new page, the driver will wait for that new page to be “loaded” (scare quotes intentional) according to the page load strategy until the page load timeout.
The page load strategy defaults to “normal”, or waiting for the document’s readyState to be complete. If you set it to “none” in the capabilities requested during driver instantiation, the driver will not wait at all. Choosing that route would mean you would need to handle all synchronization for pages being loaded. Note there is a third page load strategy, “eager”, but at the time of this writing, not all WebDriver implementations (most notably chromedriver) support it.
You can adjust the page load timeout at runtime in your Selenium code. One approach might be to lower the timeout to a relatively low value for the duration of clicking on this particular element, then restoring it to its prior value afterward. The drawback here is that you will have to catch the timeout exception that is thrown when the page load times out before continuing.

Selenium Click with actions vs click without actions

I was testing a scraper and I noticed that when using:
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
tab.click();
I receive this message constantly:
1536868112230 Marionette DEBUG [6442450945] Canceled page load listener because no navigation has been detected
However when I use this instead:
Actions builder = new Actions(driver);
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
builder.click(tab).perform();
I don't receive a message every time I sift through the tabs or click on new links.
Am I receiving this message because I am not emulating the user behavior thus I get thrown this message because it was waiting for a user click on the tab? And since I am scraping and not testing, is it better to just use the former instead of the latter since I don't need to interact with the GUI anyways?

I need to add a wait till i get a response from angular http request with selenium-java

I am very new to selenium , i am trying to write a code which will login into my website and check for the dropdowns and click on the button.
once after clicking the button a http angular request is fired and result is displayed. my requirement is to wait for the response and check the response with selenium and then logout from my website
driver = new ChromeDriver();
driver.get("https://url");
driver.findElement(By.id("userId")).sendKeys("USERID");
driver.findElement(By.id("password")).sendKeys("PASSWORD");
driver.findElement(By.id("Submit")).click();
Select dropdownA = new Select(driver.findElement(By.id("mySelectA")));
dropdownA.selectByValue("2");
Select dropdownB = new Select(driver.findElement(By.id("mySelectB")));
dropdownB.selectByValue("5");
driver.findElement(By.id("findroute")).click();
/***/Here i need to wait for the angular http request to reply and check for the data displayed***
driver.findElement(By.id("userTemp")).click();
driver.findElement(By.linkText("Logout")).click();
driver.close();
Firstly, lets add some implicit wait after initiating the driver. Now this is the default time selenium waits for any element including angular.
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Now lets consider element A appears after some action performed by the user. Element A is angular i mean. Now check for the existence of the element.
driver.findElement(By.id(""));
//Perform the required operations after finding the element
The script waits for 60 seconds for the angular element to appear. If now we will be getting element not found exception.
Secondly,, we can make use of Explicit wait to achieve the same.
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));
or
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
Hope this helps you. Thanks.

Notification(Alert) comes randomly throughout application in selenium webdriver

I am using selenium webdriver with Java language. I am facing an issue that notification alert appears randomly through out application. Basically these alert have some information as it is functionality .
As script performs its steps but suddenly these alert appears on the screen and my script would fail due to alert message.
Please give your suggestions, How can we handle this type of alert which appears randomly on any window?
Below are two points come in my mind to handle this scenario:
I will check Alert appears or not at every step (after click or
other action) but it increases my execution time.
Is there any way, we keep monitoring that Alert appears or not. If
Alert appears just close the Alert if not then keep execute the
steps of scripts.
Please suggest any workaround to handle such Alert so that our scripts do not fail.
This is the same scenario that we automate mobile application using Appium tool and suddenly any advertisement come on the screen.
It would be good if any one provide java code to handle this type of scenario.
Thanks in advance!!
If you want that unnecessary alert not appear during your script execution you can override you alert function before executing your script by using JavaScriptExecutor as below :-
JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("window.alert = function () { return true}");
You can execute this script once every time when your page is loaded. this script will override your alert functionality and the alert will never occur.
I suggest you this script run when if your test is not depend on alert because after execute this script the alert will not be appear on the page.
other than you can handle alert as below :-
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
Now if you want to accept alert, you can use :-
alert.accept();
and for cancel, you can use :-
alert.dismiss();
Note :- if alert is not present by using WebDriverWait it will throw TimeoutException.. so you need to handle it.
Edited..
For Appium automation to solve this problem, you can use a desired capability specifically designed to handle these alerts.
You can either always accept or always dismiss the alerts with these desired capabilities :-
autoAcceptAlerts = true
...
capabilities.SetCapability("autoAcceptAlerts", true);
or
autoDismissAlerts = true
...
capabilities.SetCapability("autoDismissAlerts", true);
Furthermore, some of the older versions of Appium haven’t worked with this solution, so you might want to try a small workaround with this :-
driver.SwitchTo().Alert().Accept();
For more info follow this
Hope it will help you..:)
You can call this method wherever you are getting the alert.This method will accept the alert.
public void checkAlert(){
if(ExpectedConditions.alertIsPresent() != null){
driver.switchTo().alert().accept();
}
}

How to call HTML / JSP code from Java Thread

In my web application, I have a JSP page to which allows a user to upload a CSV file.
When user submits this upload form, control goes to a Struts2 action,
from this action I am starting a new Java thread which handles the reading and processing of this CSV.
I don't want to block the view of application as CSV can be very large so thread handles the uploading and reading of CSV
and action returns to some confirmation view page.
Now I want to notify user when thread completes its execution and uploading of file is done.
As soon as thread finishes its execution, I want to pop a javascript alert in my web app
with a message "Upload complete. Click here to view".
My current approach(not good) is to pass session object to thread and set a completion flag in thread.
Action
//----
new Thread(new UploadThread(session)).start();
//----
UploadThread.java
//----
try {
//Reading and processing CSV
} catch() {
//Exception handling
} finally {
//Set flag in every case
session.setAttribute("uploadFlag","true");
}
//----
In the meantime, I set a JS method from one of my JSPs to execute in every 5 seconds. In every 5 secs,
this method checks "uploadFlag" from session and if its value is set, it pops javascript alert.
This is working but session object should not be in Thread.
Is there some way to achieve this alert from Thread's finally block.
I did some googling and found these SO posts-
Open local html page - java and
Getting java gui to open a webpage in web browser
Apart from these I've tried to use java.net.URL + openConnection and HttpClient also.
But all these return output of target URL in stream.
Kindly suggest.

Categories