3

nothing is working well for it. suggest me better option. due to fast speed i can't detect execution in better way. here is my code:

public static void main(String[] args) {

    //**********************************open ff
        WebDriver driver=new FirefoxDriver();
    //**************************************maximize ff 
        driver.manage().window().maximize();

            Logger log = Logger.getLogger("devpinoyLogger");
        driver.get("http://navvitistgvm.cloudapp.net/nvrppluginassist/Account/Login");

            log.debug("entring username");      
        driver.findElement(By.xpath("//*[@id='UserName']")).sendKeys("rpadmin");

            log.debug("entering password");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("Password123");

            log.debug("Clicking login");
        driver.findElement(By.xpath("//*[@id='loginForm']/form/div[4]/div/input")).click();

            log.debug("Clicking voucher");
        driver.findElement(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")).click();



                log.debug("selecting search_voucher");

                 List<WebElement> elements=driver.findElements(By.id("VoucherType"));
                 //elements.get(0).click(); //GV
                 elements.get(1).click();  //GC
                 //elements.get(2).click();//AP

                 driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

            driver.findElement(By.xpath("//*[@id='main']/form[2]/div[2]/input[4]")).click();
            driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
                driver.findElement(By.xpath("//*[@id='main']/div[1]/span/a")).click();
    }
    }

nothing is working well for it. suggest me better option. due to fast speed i can't detect execution in better way.

  • Are you saying that your program is too fast and asking how you can slow it down? – Kenney Nov 20 '15 at 20:14
  • 1
    can you add a bit more detail to your question so I can better answer for you. What actually is the issue you have? – cconolly Nov 20 '15 at 20:15
  • yes. due to that sometimes it gives no such element found exception. –  Nov 20 '15 at 20:15
  • @cconolly slenium web driver clicks or entering every detail fast. i need to slow down whole process. –  Nov 20 '15 at 20:17
  • you want every command to run with more time between them? You say your getting no such element exceptions suggesting the test runs a bit too early rather than too fast. Which elements get this exception? – cconolly Nov 20 '15 at 20:19
  • @cconolly right now the above code is working fine. but if i add more feature to it then some time it gives no such element .yes i want every command to run with more time between them. –  Nov 20 '15 at 20:23
  • You don't really want to slow everything down. You want the script to run as fast as possible but without error. You need to debug your code and find out where and when the errors are occurring and add code to handle the situation. – JeffC Nov 20 '15 at 23:16

6 Answers6

3

For u who is looking for help in 2020, i recommend this page: https://www.selenium.dev/documentation/en/webdriver/waits/

i used the Explicit wait method! helped aloot

matmany
  • 151
  • 1
  • 6
1

So this isn't a direct answer to your question, slowing down the process, but this is an answer to the problem I think your having.

You don't need the steps to run slower, but what you do need is to make sure that you dont run any steps untill the page has properly loaded.

You can use WebDriverWait and visibilityOfElementLocated to solve this issue.

I've added a couple of lines to your code

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


public static void main(String[] args) {

  //**********************************open ff
  WebDriver driver = new FirefoxDriver();
  WebDriverWait wait = new WebDriverWait(driver, 30);
  //**************************************maximize ff 

  driver.manage().window().maximize();

  Logger log = Logger.getLogger("devpinoyLogger");
  driver.get("http://navvitistgvm.cloudapp.net/nvrppluginassist/Account/Login");
  // driver.get should block the execution of following steps untill the
  // page has loaded so this line below in theory shouldnt be needed.
  // Try it in your code and see for yourself
  // Wait till the username field is visible.
  wait.until(visibilityOfElementLocated(By.xpath("//*[@id='UserName']"))));

  log.debug("entring username");      
  driver.findElement(By.xpath("//*[@id='UserName']")).sendKeys("rpadmin");

  log.debug("entering password");
  driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("Password123");

  log.debug("Clicking login");
  driver.findElement(By.xpath("//*[@id='loginForm']/form/div[4]/div/input")).click();

  // I assume here is your other issue, when clixking login the view changes
  // and your running this next command before the view has properly refreshed.
  // add a wait here as well.
  wait.until(visibilityOfElementLocated(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")));

  log.debug("Clicking voucher");
  driver.findElement(By.xpath("html/body/nav/div[2]/div[2]/ul/li[2]/a")).click();

  log.debug("selecting search_voucher");

   List<WebElement> elements=driver.findElements(By.id("VoucherType"));
   //elements.get(0).click(); //GV
   elements.get(1).click();  //GC
   //elements.get(2).click();//AP

   driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);

  driver.findElement(By.xpath("//*[@id='main']/form[2]/div[2]/input[4]")).click();
  driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
      driver.findElement(By.xpath("//*[@id='main']/div[1]/span/a")).click();
}
}

If you really want the code to execute "slower" something which I wouldn't recommend (how slow do you make it?) much better would be to explicitly test using the method I showed you above and only adding waits where you know you need to (eg page/view loading as a result of an action)

An approach would be to wrap actions and add a timeout eg:

public void waitAndClick(Xpath) {
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.findElement(By.xpath(Xpath)).click();
}

Give the first approach a try though, its a much better solution.

cconolly
  • 366
  • 1
  • 9
  • yes cconolly i want to slow down the code execution .can u help me in that?? –  Nov 20 '15 at 20:41
  • updated the answer to give an example of how you could do what you want, I'd still recommend using the first solution its much better and will stop you getting those exceptions – cconolly Nov 20 '15 at 20:55
0

If you're getting no such element exceptions after you do some action and before another then use an explicit wait (http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp).

Please don't add a bunch of sleeps!!

  • i have tried the link u gave. still its not working. can u give me suggestion according to my code??? –  Nov 20 '15 at 20:27
  • It seems like there is some change in response to the click so you need to wait for the some change in state after the click events. For example, if the voucher button/link is not even in the DOM until after you click login, you have to wait for that to become present. – user2157249 Nov 20 '15 at 21:31
0

You need to use fluent wait for a page to load or after every click event wait for a particular element to load and then perform certain selenium api calls:

You can find docs on how Fluent Wait of selenium works over here https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html

Akshar
  • 1
0

You can try Thread.sleep(3000) (for 3sec) if implicity and explicity does not work as expected..

i hope below link clarifies on execution speed, setSpeed in Selenium WebDriver using Ruby

Community
  • 1
  • 1
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

I was stumbling against the same in my own code. I tried in my application these codes from the selenium API:

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
wait.until(visibilityOfElementLocated(By.xpath(xpath))); 

It worked a way better, but still I was getting errors like xpath is not located. So i figured out those waiting methods from Selenium were somehow too fast or my elements didn't showed up on time.

Now with this code it really works fine for my application:

//extra waiting due to the limitiations of selenium, selenium sometimes loads too fast
        Random ran = new Random();
        //The integer x is now the random number that has a possible outcome of 800-1200.
        int x = ran.nextInt(401) + 800;
        try {
            Thread.sleep(x);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Gerrit-Jan
  • 59
  • 1
  • 9