简体   繁体   中英

How to verify that clicked on URL is being opened before redirect in Selenium Web Driver?

I am using Selenium Java WebDriver to create my tests. The test I'm trying to automate is as follows: 1) Get URL from CSS Element I'm hovering on. 2) Click on that Element (it's a link). 3) Verify that opened URL is what expected.

Step 3 would be easy if opened URL was actually the same as the one obtained in step 1. However, the URL in my case gets redirected until it finally opens, thus my test case fails.

My current implementation: 1) With this method I get the URL:

    public String getHrefFromElement(String selector) {
    return driver.findElement(By.cssSelector(selector)).getAttribute("href");
}

2) I click with this method on the selector:

    public void click(String selector) {
    driver.findElement(By.cssSelector(selector)).click();
}

3) After clicking I'd get current URL and write basic assertion for comparing Href from Element obtain in #1 with current URL (it would fail).

    public String getCurrentURL() {
    return driver.getCurrentUrl();
}

Question: If clicked on URL gets redirected, could I somehow verify that URL was being opened before redirect? How could I get URL state from the very start before the redirect before page loads?

You will need to add a wait in order to make sure the page has loaded (and redirects have finished).

I would recommend reading a bit about the Page Object pattern/approach to testing as it will help with abstracting this easily.

You should add an event listener to the window URL. That way every time it changes you can compare it to the stored value in your code.

Comment above is correct, I'd like to add more info to it.

You can try to add listeners to your code it will help you a lot only with this issue.

I run my tests with C# so I don't really know all the things in Java but hope this will help you https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/events/EventFiringWebDriver.html .

What you need to do is to create a class where you'll add events listeners such as "Element Clicked" and "Navigation" and "Element Found" etc.

Having this you'll be able to do some actions in "Navigating" event. It will help you to get URL that you are being navigating to.

On C# my listener looks like:

public static void Navigating(object sender, WebDriverNavigationEventArgs e)
{
     Console.WriteLine("Will navigate to " + e.Url);
}

you can try like this below i have presented my answer taking the example of google home page (Search page)

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.omg.Messaging.SyncScopeHelper;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import junit.framework.Assert;

public class WalmartDatePicker {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.getProperty("webdriver.chrome.driver","C:\\Users\\rajnish\\Desktop\\ImpFolder\\SeleniumJar\\chromedriver_win32 (1)");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://www.google.com");

        // finding all urls on the google home page :

        List<WebElement> alllink = driver.findElements(By.tagName("a"));

        for(int i = 0 ;i<alllink.size();i++){
            // printing links present on th egoogle home page.
            if(alllink.get(i).isDisplayed() == true){
                String urlp = alllink.get(i).getAttribute("href");
                System.out.println("Links are : " + urlp );
                // now click link one by one 
                alllink.get(i).click();
                // after clicking we move to the url 
                // here we can do two things to verify 

                // 1. verify title of the page and navigate back. 
                /* System.out.println(driver.getTitle());
                 Assert.assertEquals(driver.getTitle(), driver.getTitle());
                 driver.navigate().back();*/

                //2 verify before url with current opened url
                 Assert.assertEquals(urlp, driver.getCurrentUrl());

                 alllink = driver.findElements(By.tagName("a"));
            }
        }
    }
}

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