Try/Catch statement causing performance issue in Selenium Java??

2,327 views
Skip to first unread message

sirus tula

unread,
Nov 7, 2012, 11:16:35 AM11/7/12
to seleniu...@googlegroups.com
Hello Selenium Experts,

After migrating to Selenium Java from Selenium c#, i found that try/catch exceptions are affecting performance as it's making the
execution very slow.

Below is my isElementPresent boolean method to see if the element is present.

//common isElementPresent Method in Common.java file
  public boolean IsElementPresent(By by)
    {

        try {
            driver.findElement(by);
            return true;  // Success!
        } catch (Exception ignored) {
            return false; 
        }
    }


//main script method

@Test
public void myMethod() throws Exception
{

Common common= new Common(driver);

 if (common.isElementPresent(By.id("")))
   {  
           //do sth
    }
 } 


This works perfect when it finds the element in the page but when it doesn't,  it takes about 10 seconds to exit out of the if statement.

Not sure why it's happening in Java, i tried same method with different syntax for c#, I never saw any performance issue.


Did any of you guys experience this?


Any helps/suggestions is appreciated.


--
 
- "If you haven't suffered, you haven't lived your life."
 
Thanks,
 
Sirus

Karthik Kulkarni

unread,
Nov 7, 2012, 11:56:12 AM11/7/12
to seleniu...@googlegroups.com
try catch block can never cause performance issues .


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

sirus tula

unread,
Nov 7, 2012, 12:22:18 PM11/7/12
to seleniu...@googlegroups.com
Like i said, When i debug line by line,  try statement looks for the element and if it finds, it executes instantly if not, it takes probably 10 seconds to exit out of the catch statement. 

I didn't experience the time lapses in c#.

Thanks

Karthik Kulkarni

unread,
Nov 7, 2012, 12:27:46 PM11/7/12
to seleniu...@googlegroups.com
depends on a lot of things . what u have written in d code and all . if u think
C# was giving u better perf go with it

sirus tula

unread,
Nov 7, 2012, 1:27:43 PM11/7/12
to seleniu...@googlegroups.com
That's why i gave my code on top of my post. You might be right, it might not be try/catch but selenium java itself.

What i think it's doing is that it's looking for that element in the whole page source and that's why it's taking time. If the element was there, it would find it instantly but if not, it's scanning the whole page source and taking lot of time. It wasn't taking that much time in selenium c#.

If i had the option to chose between two, i wouldn't have even tried java.

Any other suggestion/advices by folks out there?

-Thanks

Nikolay Georgievskiy

unread,
Nov 7, 2012, 6:01:30 PM11/7/12
to seleniu...@googlegroups.com
They cause performance issues but not those which you can notice in selenium tests.

Regarding the topic I can provide a link http://seleniumhq.org/docs/04_webdriver_advanced.html

Mark Collin

unread,
Nov 8, 2012, 1:36:48 AM11/8/12
to seleniu...@googlegroups.com

My first suggestion would be bin the try/catch, modify your code to be:

 

Public Boolean isElementPresent(By by){

                return driver.findElements(by).size() > 0

}

 

There could be many reasons for slow code, you haven’t shown us everything so we don’t know what else you are doing.  Also bear in mind that during debugging things will be slower.

--

sirus tula

unread,
Nov 8, 2012, 9:44:56 AM11/8/12
to seleniu...@googlegroups.com
Thank you everyone foryour suggestions.

Mark, i tried that one as well but still no gain in performance.

What i understand is that, if i try to find the element that is not present, it scans the whole page source and is taking long time
to throw back to us that the element is not present.

Since my work code cannot be shared, i tried with simple google test which is still having same the problem when it executes isElementPresent
method.

Below is my code.


package Functional_test_Firefox;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;



public class Google_Test {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.gmail.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }


    @Test
    public void Create_New_Account() throws Exception {

        driver.navigate().to(baseUrl);

//takes about 10 seconds to exit out of this if statement
        if (isElementPresent(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))        //checking for Compose button
        {
            System.out.println("Already logged in");
        }

        else
        {
            System.out.println("Loggin in");
            driver.findElement(By.id("Email")).sendKeys("yourus...@gmail.com");
            driver.findElement(By.id("Passwd")).sendKeys("Hello123");
            driver.findElement(By.id("signIn")).click();
            if (!isElementPresent(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))         //checking for Compose button
            {
                System.out.println("Failed to login");
                fail("Unable to login");
            }
        }


    }

   //tried both of these methods but still slow performance
   /* public boolean isElementPresent(By by)
    {

        try {
            driver.findElement(by);
            return true;  // Success!
        } catch (Exception ignored) {
            return false;
        }
    }*/

   
    public boolean isElementPresent(By by) {
        return driver.findElements(by).size() > 0;
    }

}

Thank you everyone. Keep the suggestion/feedback coming.


On Thu, Nov 8, 2012 at 2:20 AM, Kenny Chua <spa...@gmail.com> wrote:
Yes, upvote! This is almost certainly the cause of the symptoms described. Debugged and fixed many along my travels in Selenium


On Thursday, November 8, 2012 1:11:55 PM UTC+11, Andrew Muraco wrote:
Are you setting an implicit wait time on the driver?
A line like this:
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Would be a culprit of the delay.

An alternative way to get around this issue is to do:
  return driver.findElements(by).size() == 0;
and not bother with the try/catch at all.

-Andrew

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/9K4N2caWegIJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Collin

unread,
Nov 8, 2012, 4:33:47 PM11/8/12
to seleniu...@googlegroups.com

For a start you are setting implicitly wait for 30 second (never use implicitly wait, that’s only used by people who aren’t sure what they are doing).  That will slow things down in general as for every call it will wait up to 30 seconds.

 

I would write that code as:

 

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.WebDriverWait;

 

import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

 

public class Google_Test {

 

  private static final String baseUrl = "http://www.gmail.com/";

 

  private static WebDriver driver;

  private static WebDriverWait wait;

 

  @Before

  public void setUp() throws Exception {

    driver = new FirefoxDriver();

    //Set up a wait object that waits a maximum of 15 seconds, checking the condition every 100 milliseconds

    wait = new WebDriverWait(driver, 15, 100);

  }

 

  @Test

  public void Create_New_Account() throws Exception {

 

    //Ensure we are in a logged out state when starting test

    driver.manage().deleteAllCookies();

 

    //Navigate to website

    driver.navigate().to(baseUrl);

 

    //TODO create a login page object for all the below

    //Log In

    driver.findElement(By.id("Email")).sendKeys("yourus...@gmail.com");

    driver.findElement(By.id("Passwd")).sendKeys("Hello123");

    driver.findElement(By.id("signIn")).click();

 

    //Check we are logged in

    WebElement composeButton = wait.until(visibilityOfElementLocated(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")));

 

    //TODO do something with the compose button?

sirus tula

unread,
Nov 8, 2012, 11:12:24 PM11/8/12
to seleniu...@googlegroups.com
First of all, thank you mark for your suggestion and taking time to create a new test to help me out.

Now, for the wait, i just used implicit wait for simple example

Now regarding the tests, i just used the posted one as a sample test so the test is not exactly like mine but the problem is similiar as you can probably reproduce the slowness.

Also, I see your tests covers the positive scenario but in my tests, there are three or four functionalities that depends upon the scenario like these.

1. If element1 is present
            //do sth.
2. else if element 2 is present
          // do sth.
3. else if element 3 is present
         //do sth.
4. else
          // print page not found.

So it triggers various functionality depending upon the presence of element so i had to make isElementPresent method like above but like i said, it's slowing the execution.

What i understand is that, if it finds element, it executes instantly but if it doesn't, it scans the page source and takes about 10 seconds to throw back that it didn't find the element.

The weird thing is that, i didn't see slow performance in Selenium c# though.

Any other feedback/suggestion by other folks is appreciated.

Thanks

Mike Riley

unread,
Nov 9, 2012, 12:35:50 PM11/9/12
to seleniu...@googlegroups.com
Assuming the code is identical in behavior for your Java and C# versions I can't imagine why you would see different behavior like this.  If you could provide two code variations that would show this behavior it would be worth filing an issue with that info, because identical code in each language should result in close to the same amount of time to execute.

One thing about your if-else if testing is that in the worst case scenario you have done 3 complete scans of the DOM before it fails, so that would be the case that would take the longest time.

Can I assume that in each case you are testing for an element specific to a particular page here, and by not finding it you consider none of the expected pages to have loaded?

If that is the case, how about simply checking for the page title?  If the title is different for each page it would be a single call to get the title (no DOM search required) and you would then test that for a match or failure to match.  That would only work if each page variant has a different title, of course.  Just a thought about how you might consider changing your code to get around this behavior.

Mike

    driver.findElement(By.id("Email")).sendKeys("yourusername...@gmail.com");

            driver.findElement(By.id("Email")).sendKeys("yourusername...@gmail.com");

sirus tula

unread,
Nov 9, 2012, 5:39:05 PM11/9/12
to seleniu...@googlegroups.com
Thank you Mike for your suggestion. Like you said, i think it's bug for selenium Java as  I compared with same selenium script in c#. 

When I run the same script in java, it takes more than twice time as shown below in the table.

Script          Test_script         Time
Java              Google_tests     57.5 secs
C#       Google_tests           20 Secs.

Below is the script in both language as shown below.

#C Selenium Script

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using NUnit.Framework;
using Selenium2;
using OpenQA.Selenium.Firefox;



namespace Firefox_Tests.Functional_Testing
{
    public class Google_Test
    {

        public IWebDriver driver;
        public string browser { get; set; }
        public string baseURL;
        private StringBuilder verificationErrors;
      

        [SetUp]
        public void TestSetUp()
        {
            driver = new FirefoxDriver();
            verificationErrors = new StringBuilder();
            baseURL= "http://www.gmail.com/";
        }


        [Test]

        public void Create_New_Account()
        {

            driver.Navigate().GoToUrl(baseURL);
            if (isElementPresent(By.CssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))             //checking for Compose button
            {
                Console.WriteLine("Already logged in");
            }

            else
            {
                Console.WriteLine("Loggin in");
                driver.FindElement(By.Id("Email")).SendKeys("yourus...@gmail.com");
                driver.FindElement(By.Id("Passwd")).SendKeys("Hello123");
                driver.FindElement(By.Id("signIn")).Click();
                if (!isElementPresent(By.CssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))         //checking for Compose button
                {
                    Console.WriteLine("Failed to login");
                    Assert.Fail("Unable to login");
                }
            }
        }


        [TearDown]

        public void TestTearDown()
        {
            driver.Quit();
        }

        #common isElementPresent method

        public bool isElementPresent(By by)
        { 

            try
            {
                driver.FindElement(by);
                return true;  // Success!
            }
            catch (Exception ignored)
            {
                return false;
            }

        }


    }
}


//Java Selenium Script

package Firefox_Functional_test;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;



public class Google_test {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.gmail.com/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    //@Ignore(value = "testing one by one")
    @Test
    public void Create_New_Account() throws Exception {
        //Instantiate other classes

        driver.navigate().to(baseUrl);
        if (isElementPresent(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))             //checking for Compose button
        {
            System.out.println("Already logged in");
        }

        else
        {
            System.out.println("Loggin in");
            driver.findElement(By.id("Email")).sendKeys("yourus...@gmail.com");
            driver.findElement(By.id("Passwd")).sendKeys("Hello123#");
            driver.findElement(By.id("signIn")).click();
            if (!isElementPresent(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")))    //checking for Compose button
            {
                System.out.println("Failed to login");
                fail("Unable to login");
            }
        }


    }

    public boolean isElementPresent(By by)
    {

        try {
            driver.findElement(by);
            return true;  // Success!
        } catch (Exception ignored) {
            return false;
        }
    }

/*
    public boolean isElementPresent(By by) {
        return driver.findElements(by).size() > 0;
    }
    */
@After
   public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}
}


About the if-else statement for title, it's happening in the same page and one of the condition is that let's say if there are 5 items already, then the button is disabled so i have to test for that one as well and if not, just click that button to create a new instance and also sometimes print out the error message after clicking the button so i also check for the error message in the if-else statement.


Regarding the defect, I think it's a bug related to selenium java for performance.

Can any of the selenium developers confirm this?

Any help/feedback is appreciated.

Thanks




To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/1KJ1Do5KzkwJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mike Riley

unread,
Nov 13, 2012, 12:49:50 PM11/13/12
to seleniu...@googlegroups.com
I only had time for a quick glance at your code, but in the Java version you are doing an Implicit Wait of 30 seconds and you are not doing the same in the C# code.  That looks like it would definitely change the behavior to me.

What happens if you remove that from the Java version, or add it to the C# version?

Again, I could only glance at it, but the rest seemed to be the same code except for that.

Mike
                driver.FindElement(By.Id("Email")).SendKeys("yourusername...@gmail.com");

    driver.findElement(By.id("Email")).sendKeys("yourusername@gmail.com");

            driver.findElement(By.id("Email")).sendKeys("yourusername@gmail.com");

sirus tula

unread,
Nov 13, 2012, 1:39:07 PM11/13/12
to seleniu...@googlegroups.com
Hi Mike, Sorry i forgot to comment that code.

It still has slow performance in Java compared to C#. The results below is the code after commenting out the wait command.

-Thanks



To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/Rt_dcSSYQbwJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Collin

unread,
Nov 13, 2012, 3:42:47 PM11/13/12
to seleniu...@googlegroups.com
Write a test for each scenario.

A test should be targeted to check a specific journey or piece of
functionality, it's not a swiss army knife, you don't want to end up with
one of these...

http://www.firebox.com/product/1861/Wenger-Giant-Swiss-Army-Knife

I would suggest you write a test for each scenario rather than trying to
cater for multiple scenarios in one test.

If your test is going down different paths all the time how do you know
which path you have tested? How do you know your application is fit for
purpose if you have potentially only tested 1/3 of the functionality?



-----Original Message-----
From: seleniu...@googlegroups.com
[mailto:seleniu...@googlegroups.com] On Behalf Of sirus tula
Sent: 09 November 2012 04:12
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Try/Catch statement causing performance
issue in Selenium Java??

First of all, thank you mark for your suggestion and taking time to create a
new test to help me out.

Now, for the wait, i just used implicit wait for simple example

Now regarding the tests, i just used the posted one as a sample test so the
test is not exactly like mine but the problem is similiar as you can
probably reproduce the slowness.

Also, I see your tests covers the positive scenario but in my tests, there
are three or four functionalities that depends upon the scenario like these=

Reply all
Reply to author
Forward
0 new messages