I want to run a java based automation test (using testng) on a windows application 24*7. The code is working fine but after some time the winappdriver responds very slowly and selenium's basic operation like clicking element is taking a lot of time unless I restart my windows machine. WinAppDriver gets stuck on the state shown in image.
winappdriver state
I tried driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); so that if it could not find an element within 2 seconds then it should throw an exception but it is not working. I just want that if it is taking too long to perform an operation then it should throw an exception and my Retryanalyzer will restart the test but I dont know how to achieve this.
try {
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
do action
CustomReporter.log("\n action");
} catch (NoSuchElementException e) {
call reset method
CustomReporter.log("\n reset");
}
But using implicitlyWait is not a very good approach in general
Did you check memory usage? It would be better to check it
Related
I am doing something like this in Java Selenium (Numbers within parenthesis are line numbers. They are not in the actual code.)
(1) driver.get(URL1);
(2) driver.findElement(By.xpath(xpath)).click();
(3) WebDriverWait wait = new WebDriverWait(driver, 15);
(4) wait.until(webDriver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete"));
(5) driver.get(URL2);
But 5 driver.get(URL2); is ignored when executing this code but if I step (debug) through these lines it works fine.
I added two lines like this
System.out.println(new Date());
before 3 and after 4 and the result was weird:
When running the program "normal" these printouts always return at the same time. In other words, the wait is returning within 1 second. However, when debugging and stepping through these lines, there is a 4-5 second long delay between these timestamps, even if I step as fast as I can.
I suspect that 3-4 that is supposed to verify that the page has loaded are executed so fast so that they check the state of the previous (current) page (URL1) rather than waiting for the page resulting from the click. And then the browser is busy loading a new page and therefore can't handle the command sent in 5.
How do I fix this?
I have read dozens of questions here on SE on how to make Selenium wait for a page to load but I am already using one of the most common solutions (line 3-4) suggested in these questions.
I have worked with Selenium and Web driver in Python for some time. I have just used the delay() method in Python for a rest. Can you upload the whole code or something to try to find out how to help you? Thanks!
I'm currently running some Cuke test with Selenium that hinge on preconditions in the system. A given run can include 1 or more Features. Features check for certain preconditions at the start of the run, for instance whether or not it can find the proper driver.exe file. If some of those preconditions fail, I would like to kill the run completely inside of a catch block an prevent any other scenario or Feature from being checked as they will all fail anyway. Is there a function or set of functions to accomplish this?
try {
//Gonna check for things here
} catch(Exception e) {
//Something went wrong, kill this thread.
}
I would consider a before step in Cucumber. It will be executed before each scenario in that particular feature file. This would result in the check being performed before each scenario. If you need, set a static flag that you can examine and fail fast if needed.
I am writing a program in java which can start up applications such as, for example, firefox.
Edit: This program is for linux, specifically ubuntu.
It's easy to start the program:
Runtime.getRuntime().exec("/usr/bin/firefox");
However, I want to retrieve details from the window once it is fully opened or running.
At the moment I'm just calling:
Thread.sleep(delay);
To make sure the window is ready, but this is a poor solution. Different windows requiring different delays is a problem.
Messy.
So my question is, is there any way that I can be notified when firefox (or any other external application for that matter) is fully setup? I don't think I could use Process.waitFor() because the Process won't be finished until firefox is closed.
Thanks in advance!
Update: Process.waitFor() doesn't work. I have tried it and it only returns when firefox is closed, not when it is fully setup. Just for anyone trying it themselves, if another firefox window is already open it will work (which fooled me at first) but if there is no existing window it won't!
You can use Process#waitFor to wait till the command gets executed and then check the exitValue like this:
Process p = Runtime.getRuntime().exec("/usr/bin/firefox");
p.waitFor();
if(p.exitValue()==0) {
//success
} else {
// fail read error stream or out stream for possible causes
}
Ok I have been doing some more thinking and I have a reasonably satisfactory answer.
Instead of waiting until the window is ready, continually search for it with xdotool:
while(line == null){
writer.write("xdotool search --onlyvisible --name " + name + "\n");
writer.flush();
if(reader.ready())
line = reader.readLine();
Thread.sleep(1000);
}
xdotool will only print a string if it finds a window called name.
So if the reader is ready() then you know the window is open.
The Thread.sleep() is necessary because if it is not present xdotool will spit out a bad window error and the reader will read that.
However, it seems to almost be faster to use a standard delay like I spoke about above but this solution will work even for windows which take longer to load, rather than trying to guess a delay.
I'm facing a issue regarding a popup(not a browser popup an application one), and im handling it perfectly. But the problem is i dont know when it appears and by default i'm using Thread.sleep for 20 seconds in order to handle it. Now i have to cut down the time and have to handle the issue effectively.Can any one pls help me out without using Thread.sleep.
Thread.sleep(20000);
if(oASelFW.driver.findElement(By.xpath("//div[#class='fsrFloatingMid']")).isDisplayed()){
oASelFW.driver.findElement(By.xpath("//a[#class='fsrCloseBtn']")).click();
}
You need to make use of Explicit waits which will allow to wait until certain condition is satisfied - which in your case is until is element present.
WebElement whatever = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(
By.xpath("//div[#class='fsrFloatingMid']"));
Clicking on a button in my web page sometimes causes the entire page to load, and sometimes only part of it to load.
How can I call waitForPageToLoad without the page loading, and to be able to run additional commands after all elements are present, or what other command can I use, that will wait for the page to be loaded and enable me to run additional commands on the page.
(Using selenium 2.)
Clicking on a button in my web page sometimes causes the entire page to load, and sometimes only part of it to load.
I assume this is by design, and not the problem.
If you are testing, then you should know which behavior you are expecting. If you are expecting a full page load, then use clickAndWait. If you are expecting a partial load, then use click followed by waitForCondition.
You can use the wait() command to wait a specified amount of time, and continue with your actions afterward.
synchronized (driver) {
try {
driver.wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
It may be better to use clickAndWait or waitForCondition, but this is an alternative for just waiting in general.
Wait for element you want to proceed with instead of wait for page to load