1

Any help to validate a parameter in the url using selenium web driver + java

getCurrentUrl() = https://www.loca.com/usa/test/#/xxx

My url string is as above, and based on the url I need to enter a value on the loaded page, like lets say - I have zipcode field to enter zipcode value.

If url string contains 'usa' Enter zipcode as 12345 if url string contains canada Enter zipcode as Ab1233

2 Answers 2

1

If you are using Java, you can do something like:

String zipcode = "";
String currentUrl = "https://www.loca.com/usa/test/#/xxx"
//This will check to make sure USA is a part of the URL, and doesn't exist as another part of the URL.
if(currentUrl.contains("/usa/")){
   zipcode = "12345";
//Same here with Canada
} else if(currentUrl.contains("/canada/"){
   zipcode = "ab1233"; 
//You can continue pattern for any other countries
} else if(currentUrl.contains(...){
...

Just put the URL into a string, and then use the String method contains to check and see if USA in the string. If it is, use zip code, otherwise, do something else.

Sign up to request clarification or add additional context in comments.

6 Comments

i'd recommend maybe a switch statement instead, just based on the context of changing zipcodes per country. they could add more. also, should probably use .contains("/usa/") in case another part of the url has usa in it (just for safety)
This is fine until you have to support a bunch of countries... then the switch is much easier to read.
@JeffC Contains returns a boolean, so how exactly would propose you use a switch statement? From the Java docs "An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object."
Using .contains() isn't required. There are several ways you can adapt this code to use a switch. See my answer for one of them.
@JeffC While I get that you can accomplish this with a switch statement, you are making it more complicated just to make it more "readable". To me this is poor programming. Stick with what is simple. I offered a solution that was around 7 lines of code. Your solution almost triples that. You also use regex, and so on. All this to make it more readable? Good programing is providing a simple solution that does the job. Tripling the code base just to make something more readable isn't a good idea in my opinion.
|
0

I think an if then else is going to get pretty messy if you have more than just a few of them. I prefer to use a switch. I think it makes the code easier to read but maybe that's a personal preference.

String currentUrl = "https://www.loca.com/usa/test/#/xxx";
String patternString1 = ".com\\/(\\w+)\\/";
Matcher matcher = Pattern.compile(patternString1).matcher(currentUrl);
if (matcher.find())
{
    String zipcode = "";
    switch (matcher.group(1))
    {
        case "usa":
            zipcode = "12345";
            break;
        case "canada":
            zipcode = "Ab1233";
            break;
        default:
            // handle an unexpected country
            break;
    }
}
else
{
    // handle the case of a bad URL
}

// do something with zipcode
System.out.println(zipcode);

1 Comment

@BlackHatSamurai Yep, I tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.