I am using a file upload task with Selenium.
The problem here is, to upload file, it is taking 5-10 seconds time. But i have to stop the Selenium server until it uploaded completely.
Here is some example code.
selenium.type("id=Fileuploader","c:\\mypic.jpg");
selenium.click("id=submmit");
It is giving error because the selenium.click statement is executing right after the selenium.type statement without waiting for the file to upload fully.
So, what should I use here to stop the Selenium server (server has to wait for sometime)?
Asynchronous requests are always hard to track. There must be some change on the page when the file is uploaded, so look for it, wait for it.
You can try waitForCondition().
Or some sort of isElementPresent() magic
final int TIMEOUT = 10000; // ten seconds
long targetTime = System.currentTimeInMillis() + TIMEOUT;
while((System.currentTimeInMillis() < targetTime)) {
if (selenium.isElementPresent("xpath=something")) {
break;
}
}
EDIT: If there's no new element, there has to be at least some sort of change. For example, if your asynchronous upload changes a value of some hidden input element, you could test for it using getValue(), or maybe just a smart locator:
isElementPresent("xpath=//input[#type='hidden' and contains(#value,'mypic.jpg')]");
EDIT2: If the element for which we check (in this case, picture preview) is present even before the upload, then it was just not visible and we can test that by isVisible()
If worst, you can always use some Thread.sleep() in the code. But the best way is to see the advanced usage - explicit and implicit waiting
After uploading the file, new element is coming which showing the preview of the uploaded element.
So, i used
selenium.isVisible("xpath of new preview element")
Put it in loop. It will work.
Thank you.
Related
I am creating automated test cases by using selenium 3 & testng. Everything looks good, except the screenshots that are generated. Here is my piece of code to create screenshots PNG files:
file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(pngfile));
which is pretty standard way to do it, but the quality of the created PNG file is not so good.
As you can see from following PNG file. In the picture, the email value ( "....#yahoo.com"), which should be at the upper-right corner of the web-page and should be as high as the other navigation bar elements on the left side. But in the created PNG file, this item has been squeezed to the lower level, which is not what I am looking for. Any ideas ? Thanks for the help !
Make sure your window is the right size when you're opening the browser. You can do this via visual inspection or using Selenium's getSize method. I assume you're using Java, but here it is in Python as well.
Then, if the window is not of the correct size in order to guarantee that your webpage's CSS doesn't break, use setSize. Here is that method in Python as well.
Afterwards, your screenshot should look like the window does.
Please try this,
public void calltakeScreenShot(String SSName) throws Exception
{
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage img = ImageIO.read(screen);
File filetest = Paths.get(".").toAbsolutePath().normalize().toFile();
ImageIO.write(img, "png", new File(filetest + "\\Screenshots\\" + SSName + ".png"));
}
Additionally Here you just need to pre-created Screenshots folder in your Project directory. It will store it by getting absolute path of your project. Also you can manage screenshot name by passing argument.
After several day's research, here is my latest summary.
A). If I was executing the scripts, that is not in the "headless" mode. When the selenium test case is being executed, I will see a new browser session is being popped up and get to that URL, click some buttons, etc, ... till the execution is finished. In this execution, the screenshot page will be saved in good quality.
B). For the same selenium test script, if I am including one extra ChromeOptions setting, which is "--headless", I will not see any browser being brought up during the execution. And once execution is finished, I will get the screenshot with such squeezed web elements.
Comments ?
My app displays an error dialog whenever a JavaScript error occurs. This is always a bad sign, so I want to set up my tests so that, if the error dialog appears, it causes the test to fail there and then.
So I'd like to do something like (very much pseudocode!);
// start a new 'guard' thread;
start {
found = this.driver.wait(untilVisible(By.css('.myErrorDialog')), VERY_LONG_TIMEOUT);
if (found) {
// the error dialog appeared! That's bad!
throw();
}
}
// now run the test
login();
clickButton();
testBannerContains();
But I'm having trouble and I think it has to do with the way Selenium schedules actions.
What I've found is that for a single driver, I can only schedule one thing at a time, so the guard I set up early in the test blocks the body of the test from starting.
Is there a better way to handle conditions like 'this should never happen', or a way to create two independent threads in the same test?
So the problem with the code you have is that it immediately runs it and waits for a VERY_LONG_TIMEOUT amount of time for that error dialog to appear. Since it never does, it continues to wait. You have already discovered that is not what you want... ;)
I haven't done anything like this but I think you want a JS event handler that watches for the event that is triggered when the error dialog appears. See the link below for some guidance there.
Can my WebDriver script catch a event from the webpage?
One option would be to watch for that event to fire and then store true (or whatever) in some JS variable. Before leaving a page, check to see if the variable is set to true and if so, fail the test. You can set and get JS variables using JavascriptExecutor. Some google searches should get you all you need to use it.
I'm using Eclipse/Java and interfacing with the Selenium chrome webdriver.
The code I'm executing is
Sting sValue = item.getAttribute("innerHTML");
If I am stepping over this code it works fine.
Otherwise, if I just run the code. It throws a NUllPointerException.
What gives? Any ideas?
Thanks!
It's possible the page doesn't have enough time to load when you run straight through, but when you step with the debugger it's just enough extra time for the page to finish loading and avoid the NullPointerException. Try adding a wait, as described in the accepted answer for this question: Getting Selenium to pause for X seconds.
This is not a real answer, it's just a total hack! I have to get some things done so I'll investigate predicates and WebDriverWaits later.
I was creating a list of webelements:
List wElements = getElements()
Then I was trying to get the innerHTML from each webelement in the list.
It works fine in debug mode, but when I execute the code it fails to gather the information IN SOME OF THE ELEMENTS in the collection. So what I did was retrieve the same list a second time.
List wElementsB = getElements()
Then I retrieved the innerHTML out of the wELementsB. Works fine.
I tried sleeping and telling the webdriver to wait. but those failed as well.
Thanks Guys!
I read about user extensions and extending selenium but am wondering how to call a command from within a custom command I'm creating.
I added a file similar to the following to Selenium core extensions (user-extensions.js) in Selenium IDE Options.
// selenium-action-example.js
Selenium.prototype.doExample = function() {
this.doOpen("/"); // doesn't waitForPageToLoad like the command does
// These two commands are equivalent to the clickAndWait command. NOT!
// For proof, see the filterForRemoteControl function:
// http://code.google.com/p/selenium/source/browse/trunk/ide/src/extension/content/formats/formatCommandOnlyAdapter.js?r=8284#68
this.doClick("css=a#example");
this.doWaitForPageToLoad(); // doesn't wait at all
this.doClick("link=Example");
this.doWaitForElementPresent("example"); // error! undefined function
this.doClick("example");
};
In other words, how can I wait for things between clicks within a custom action?
Your command
this.doWaitForPageToLoad(); // doesn't wait at all
Doesn't wait as you have not specified wait time in brackets. You should write it as
this.doWaitForPageToLoad(30000); // time in milliseconds
Tour another Command
this.doWaitForElementPresent("example"); // error! undefined function
as no function is there in Selenium. whenever it waits for an element it checks that element is present or not so you should wait for time until it is visible/present.
Using For loop and ispresent commands you can do it.
Regards
Waiting for an page load does not to work in current versions of Selenium. As far as I can see, this is because the doWaitForPageToLoad defers the waiting until the end of the current Selenium IDE command, i.e. waiting for a page load stops the test execution until the page has loaded, but not the execution of the actual javascript function that this was executed in.
You will have to split your function in two at this point.
sometimes my selenium tests get timeouts these suggestions: How do you get selenium to recognize that a page loaded? did not fix my problem. It mostly happens at the beginning of the test when i use open or openAndWait. The odd thing is the page actually gets opend but the test just stops and does not execute fruther. I use the beta-2 version and multiwindow true. I call my browsers with the custom command. I'm open for any suggestions.
thx
kuku
Have you tried putting a fixed pause (several seconds) after openAndWait? From my experience all "opens" and "clicks" in selenium (even *AndWait ones) do not guarantee you that the page is fully loaded in the browser before the next selenium command is processed. That's why I always add a small pause after such commands.
Even if you see the page being rendered in the browser, this doesn't necessarily mean everything is available to the Selenium's command processor at that moment.
This works for me in IE6, IE7, IE8, FF, Chrome:
<tr>
<td>waitForCondition</td>
<td>
(selenium.browserbot.getCurrentWindow().$$("body").length == 1) ? true : false;
</td>
<td>10000</td>
</tr>
It just polls the target window and checks for the body element. It is still susceptible to a timeout, however, but you can set that to whatever you like.
Note: the above code uses Prototype JS to get the body element. This is not required but will need some modifications if you are using another JS lib.
I had a similar problem, mine will just timeout no matter what.
I see the page is rendered completely but for some reason selenium.open(relative_url) will just timeout.
I tried setting timeout to like 120 seconds and still the same issue. Setting the speed to really slow didn't help either.
I fixed it by handling the exception with a try/catch and basically proceed with validation if the page really rendered or not (works like a charm) so my code looks like this:
try {
selenium.open(relative_url);
} catch (Exception e) {
// check if page rendered completely here
}
// continue