Testcase running very slow after opening new windo

2019-06-07 16:47发布

EVEN MORE NEW INFORMATOIN: I'm now officialy confused as to why this test is slow. I've eliminated the need for javascript as descripbed below and I've added reporting to determine where things are being delayed. these are the lines that start with "logger". I've added the timestamps behind these lines so you can see where the delay is. This is the section of code where it looks like it messes up/takes pretty long (10min every time):

wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));
logger.log(LogStatus.INFO, "Waituntill is done and element is clickable"); /** logged as successfull at 17:45:51 */
driver.findElement(By.xpath("//div[@class='CLASS']")).click();
logger.log(LogStatus.INFO, "menu visible"); /** logged as successfull at 17:45:52 */
driver.findElement(By.xpath("//*[@class='CLASS' and text()='TEXT']")).click();
logger.log(LogStatus.INFO, "menu item is available and clickable."); /** logged as successfull at 17:55:47 */

Strange thing is that if you follow the process of the test the final step (click) shows right away as this opens a new window. This is not 10 minutes later. It seems as after this step a 10 delay is built in. But there is no such thing in my code. The code above is followed by:

Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
//iterate through your windows
while (it.hasNext()){
String parent = it.next();
String newwin = it.next();
driver.switchTo().window(newwin);

driver.switchTo().defaultContent();
logger.log(LogStatus.INFO, "timestamp1 defaultcontent"); /** logged as successfull at 17:55:47 */
WebElement frame=driver.findElement(By.xpath("/html/body/iframe"));
logger.log(LogStatus.INFO, "timestamp2 find xpath for iframe"); /** logged as successfull at 17:55:47 */
driver.switchTo().frame(frame); 
logger.log(LogStatus.INFO, "timestamp3 switch to iframe"); /** logged as successfull at 17:55:47 */
assertEquals("PageTitle", driver.getTitle());
logger.log(LogStatus.INFO, "timestamp4 confirm switch to iframe succesfull"); /** logged as successfull at 17:55:47 */

NEW INFORMATION: I've studied my code and it looks like I'm not correct on where the delay occurs. it now looks like it occurs where I change the style of an element so that it shows and another element becomes clickable. I am now using the following code to change the style but this may be the problem. Is there an easier or better way to change the style of an element?

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//div[@class='button_ribbon_dropdownsection']"));
js.executeScript("arguments[0].setAttribute('style', 'left: 1520px; top: 40px; display: block;')",element);

ORIGINAL POST: I have a testcase where I click on a link that opens a pop-up. In this pop-up is an iFrame that contains fields I want to assert and fill. However, running this case takes about 10 minutes and I don't know why. Here is my code:

//this is the link I click that opens the new window
driver.findElement(By.xpath("//*[@class='value' and text()='text']")).click();

//I have inserted this section to switch to the correct window (the popup). If I don't inlcude this the focus remains on the window where I clicked the link that opens the pop-up.
Set <String> handles =driver.getWindowHandles();
Iterator<String> it = handles.iterator();
//iterate through your windows
while (it.hasNext()){
String parent = it.next();
String newwin = it.next();
driver.switchTo().window(newwin);

//this is where I switch to the iFrame in the new window in order to reach the desired elements
WebElement testframe=driver.findElement(By.xpath("/html/body/iframe"));            
driver.switchTo().frame(testframe); /** this section may take up to 10-15 minutes */
assertEquals("pagetitle here", driver.getTitle());

assertEquals("value", driver.findElement(By.id("id")).getText());
driver.findElement(By.id("id")).clear();
driver.findElement(By.id("id")).sendKeys("number");

This code does run so there is no fault in the code (that I can find) but it just takes VERY long and I can't figure out why.

I know going to the iFrame via an Xpath is not the best method but it is a commercial product and there is no id or name for the iframe so I can't navigate to it that way.

I'm using Eclipse with Selenium webdriver and Java.

3条回答
家丑人穷心不美
2楼-- · 2019-06-07 17:26

Sorry, no answers, but rather suggestions (I'd post this as a comment, but stackoverflow won't let me):

Usually I check 3 things when something is painfully slow:

  • Selenium version: they do have some slowness bugs periodically, maybe you are just lucky to have version that has this problem;
  • Selenium version vs. Browser type and version. Could be support for your browser version is not optimal on the selenium version you are using.
  • Try to run browser with reduced/disabled javascript if at all possible. Could be that some javascript does not play well, with selenium.

If it's none of the above, I would also time each statement you suspect, and see which is actually slow or if they are all equally slow (for example there's a long outstanding bug where sometimes selenium's sendKeys is slow on IE). And enable debug log on top of that, as explained here. That would hopefully give you more information on what's going on.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-07 17:33

Thanks for the suggestions Kiril. I'm posting this as an answer as a comment field is not big enought for this reply.

I'm trying to implement what you're suggesting but the two things I've tried just now dont seem to work. I've located the item using the complete xpath:

/html/body/form/div[4]/div[2]/span[6]/div[2]/div[2]/table/tbody/tr[3]/td[2]

As I figured looking in this particular table would be enough limitation for the search I tried:

driver.findElement(By.xpath("/html/body/form/div[4]/div[2]/span[6]/div[2]/div[2]/table/tbody//*[@class='value' and text()='text']")).click();

However, this results in a NoSuchElementExcepttion

I then tried to be more specific per your first suggestion and tried:

driver.findElement(By.xpath("//td[@class='value' and text()='text']")).click();

This did work but did nothing for the speed of the test, it still takes 10 minutes for it to continue.

查看更多
beautiful°
4楼-- · 2019-06-07 17:43

One thing that comes to mind is your second xpath:

//*[@class='CLASS' and text()='TEXT']

the "//*" part could be very slow. Could you try to specify the element name or path, e.g.

//div[@class='CLASS' and text()='TEXT']

or

/select/sub/tree/to/search//*[@class='CLASS' and text()='TEXT']

to limit the search?

查看更多
登录 后发表回答