0

I need to catch the URL value which is changing dynamically after clicking to login button. driver.getCurrentUrl() is saving only latest url, which is static. I need somehow to save couple of urls which are changing dynamically during the login process and save it to string or to some array. can somebody help me?

@Test
public void expireTest() {
    driver.get("https://url/default/login");
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.id("username")).sendKeys("name");
    driver.findElement(By.id("password")).sendKeys("pass");
    driver.findElement(By.cssSelector("input[value='someInputValue']")).click();
    getUrlValue();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.MILLISECONDS);

}

public void getUrlValue() {
    String urlValue = driver.getCurrentUrl().toString();
    if(urlValue.contains("expires_in")){
        String newValue = driver.getCurrentUrl().toString();
        System.out.print(newValue);
    }
Dimencion Rider
  • 145
  • 1
  • 4
  • 13
  • If I am understanding clearly, you want to capture the network traffic, there is way by using browsermobproxy, it can capture logs and save it in json format which is easy to read, If you want I can post an example using java (you can do same in c#). – Pankaj Kumar Katiyar Feb 22 '16 at 11:52
  • Possible answer here: http://stackoverflow.com/questions/6390341/how-to-detect-url-change – djangofan Feb 22 '16 at 16:56

3 Answers3

0

You can try adding the urls to List if they are not already there until you have the base url

public void getUrlValue(List<String> urls) {
    String url;
    while (!(url = driver.getCurrentUrl()).equals(url4))
    {
        if (urls.contains(url)) {
            urls.add(url);
        }
    }
}

This should insert four different urls to the list.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • No, it's not working. Problem that getCurrentUrl() cannot save the dynamically changing value; Maybe webDriver have another method to catch the value? – Dimencion Rider Feb 22 '16 at 08:55
  • @DimencionRider What do you mean? it returns the old url? – Guy Feb 22 '16 at 09:04
  • How it works: I need to save 4 URL's values: (url1, url2, url3, url4(static value)); After clicking 'login' button, application starts the dynamically loading which has three links(url1, url2, url3) that are visible around half a second. Only after that process we can see the home page and static link(url4) which is saved by getCurrentUrl() method. How can we save to array all the url's which are changing dynamically during the login process? – Dimencion Rider Feb 22 '16 at 09:27
0

First off all I'm not sure why you are doing this. Why not just wait for an element when login is complete? If you really want to do what you asked for, you could do so using explicit waits. Something like

 WebDriverWait wait = new WebDriverWait(driver, 30);
 final List<String> urls = new ArrayList<String>();
 wait.until(new Function<WebDriver, Object>() {
        public Boolean apply(WebDriver driver) {
             String url = driver.getCurrentUrl();
             if(!url.contains("uniqueWordInOriginalUrl")) {
                 urls.add(url);
             }
             if(urls.size()!=4) {
                 return false;
             } else {
                 return true;
             }
         }
 });

If you want to avoid this and just wait for an element to be visible, you could do it using internal methods in ExpectedConditions

driver.get("https://url/default/login");
driver.findElement(By.id("username")).sendKeys("name");
driver.findElement(By.id("password")).sendKeys("pass");
driver.findElement(By.cssSelector("input[value='someInputValue']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("foo"))).click();
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • Thanks, I'll try your workaround. But, the problem is, that getCurrentUrl(); getting only static url. We cannot catch the dynamic url which is changing all the time. – Dimencion Rider Feb 23 '16 at 16:01
  • Try this. The whole point of expected conditions is to keep on polling for something so hopefully this will work. Any particular reason you are doing this? – nilesh Feb 23 '16 at 16:08
  • I'm trying to catch auth_token value – Dimencion Rider Sep 28 '16 at 13:23
-1

How about a separate thread monitoring the URL every let's say 100 ms!?

Kim Homann
  • 3,042
  • 1
  • 17
  • 20