0

I got this selenium test code. Its able to load a url, login my user name and password. but problem is, after login, i can't get current url

I want to get current url so ican test and assert if i have really logged

    WebDriver driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.get("http://testingapp.workspez.com");
    driver.manage().window().maximize();

    WebElement username = driver.findElement(By.id("field_email"));
    WebElement password = driver.findElement(By.id("field_password"));
    WebElement login = driver.findElement(By.xpath("//*[text()='Log In']"));
    
    username.sendKeys("rahul@workspez.com");
    password.sendKeys("Sujeet@19");
    login.click();
    driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
    String url = driver.getCurrentUrl();
    System.out.println("url:"+driver.getTitle());
    System.out.println("url:"+driver.getCurrentUrl());
Rahul SK
  • 350
  • 3
  • 23

2 Answers2

0

your output like this You should wait for main page url because getcurrenturl getting url when entering username and password (on that quick time ) so you should wait for dashboard url

System.out.println("url:" + driver.getTitle());
    Thread.sleep(2000);
    System.out.println("url:" + driver.getCurrentUrl());
Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

You want to assert login is successful or not. So before extracting URL after login, you should wait till page get loaded completely. You can write and use below generic method to wait till page load which can be used in further scripting

    public void waitForPageLoad() {
        Wait<WebDriver> wait = new WebDriverWait(driver, 30);
        wait.until(new Function<WebDriver, Boolean>() {
            public Boolean apply(WebDriver driver){                    
                return String
                    .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                    .equals("complete");
            }
        });
    } 

Also, your using implicit wait twice in code. If you implement implicitlyWait once, it became applicable for all steps/action related to selenium. Kindly refer to this SO thread for differences between explicit and implicit wait

Kuldeep Kamune
  • 169
  • 2
  • 10