-1

today I wrote a programm that automaticaly checks if an Netflix account is working or not. But I'm struggling at a point where I need to accept all the country codes in the URL. I wanted to use something like * in linux but my IDE is giving me Errors. What is the Solution and are there better ways?

    WebUI.openBrowser('')

    WebUI.navigateToUrl('https://www.netflix.com/login')

    WebUI.setText(findTestObject('/Page_Netflix/input_email'), 'example@gmail.com')

    WebUI.setText(findTestObject('/Page_Netflix/input_password'), '1234')

    WebUI.click(findTestObject('/Page_Netflix/button_Sign In'))

    TimeUnit.SECONDS.sleep(10)

    if (WebUI.getUrl() == "https://www.netflix.com/" + * + "-" + * + "/login") {

    }
    WebUI.closeBrowser()
  • 1
    Something like `if (true)` may be? Or even better, show us some code. – lzagkaretos Jul 14 '18 at 16:04
  • So you want to check their account against a list of country codes? – Carcigenicate Jul 14 '18 at 16:07
  • Welcome to SO. See https://stackoverflow.com/help/mcve. Your question needs editing to include your code at least. – Nick Jul 14 '18 at 16:09
  • I added the code.... So I tried it with * but it isn't working. I need something that accepts everything. For exaple that https://www.netflix.com/de-en/login is beeing accepted and also https://www.netflix.com/en-en/login, https://www.netflix.com/ch-ch/login, and so on – Mauritz Funke Jul 14 '18 at 16:11
  • You know how to compare Strings in Java ? – Marged Jul 14 '18 at 16:13
  • Yes... I could use .equalsIgnoreCase but I got used somehow to the == I'll fix that – Mauritz Funke Jul 14 '18 at 16:14
  • == is virtually never the right answer for strings. Look up the Java class Pattern. – yshavit Jul 14 '18 at 16:29
  • See: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839) – Jesper Jul 14 '18 at 18:40

4 Answers4

3

So this is your attempt:

if (WebUI.getUrl() == "https://www.netflix.com/" + * + "-" + * + "/login") {

}

which fails, as you can't just use * like that (in addition to using ==, which isn't what you should do when using java). But I think this is what you want:

if (WebUI.getUrl().matches("https://www\\.netflix\\.com/.+-.+/login")) {
  // do whatever
}

which would match in whatever country you are in: any url like https://www.netflix.com/it-en/login. If within the if statement you need to use the country information, you'll might want a matcher:

import java.util.regex.*;


Pattern p = Pattern.compile("https://www\\.netflix\\.com/(.+)-(.+)/login");
Matcher m = p.matcher(WebUI.getUrl());
if (m.matches()) {
   String country = m.group(1);
   String language = m.group(2);
   // do whatever
}

Note that we're using java here, as you have the question tagged like that. Katalon is able to use also javascript and groovy, which you've also used in your single-quote strings and leaving out semicolons. In groovy, == for string comparison is ok, and it can also use shorthands for regular expressions.

eis
  • 51,991
  • 13
  • 150
  • 199
0

You could create a list of pair valid values for the country codes if you want to keep track of which country you are in, and the just compare the two strings. If you don't want to do it that way and accept everything it comes in the url string, then I recommend you using split method:

String sections[] = (WebUI.getUrl()).split("/");
        /* Now we have:
        sections[0] = "https:""
        sections[1] = ""
        sections[2] = "www.netflix.com"
        sections[3] = whatever the code country is
        sections[4] = login
        */
0

Try to solve it with regular expression on URL string:

  final String COUNTRY_CODES_REGEX =
            "Country1|Country2|Country3";
    Pattern pattern = Pattern.compile(COUNTRY_CODES_REGEX);
    Matcher matcher = pattern.matcher(WebUI.getUrl());
    if (matcher.find()) {
        // Do some stuff.
    }
AutomatedOwl
  • 1,039
  • 1
  • 8
  • 14
  • this would try to match country codes for the entire url. That's not very robust approach, a country code such as `us` can appear anywhere in very common urls. – eis Jul 15 '18 at 09:10
  • That's why you have to adjust your regex to particular case, like /us/al – AutomatedOwl Jul 15 '18 at 09:17
0

Instead of using WebUI.getUrl() == ... you could use String.matches (String pattern). Similarly to AutomatedOwl's reply you would define a String variable that is a regex logical-or separated aggregate of the individual country codes. So you have

String country1 = ...
String country2 = ...
String countryN = ...
String countryCodes = String.join("|", country1, country2, countryN);

then you have something along the lines of:

if (WebUI.getUrl().matches("https://www.netflix.com/" + countryCodes + "/login")) {
  ... do stuff
}
tdct
  • 136
  • 1
  • 5