简体   繁体   中英

selenium webdriver url after login

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());

你的输出是这样的 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());

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM