0

I have the values in this variable

private String possibleTitleNames = "Sistem ,PC ,Desktop ";

When I go on my method like this:

checkPageTitle(driver, possibleTitleNames);

I want to check if a title contains one value from this String My method for this is here

public void checkPageTitle(WebDriver driver, String titles)
    {

    String[] items2 = titles.split(",");
    String[] items1 =getRandomWayStringValues(items2);
    items1 = titles.split(",");  
    for(int i=0;i<items2.length;i++){
           boolean found = false;
           for(int j=0;j<items1.length;j++){
              if((items2[i].equals(items1[j]))){
                 found = true;
                 break;
              }
           }

           if (found) {
              Assert.assertTrue(items2[i],driver.getTitle().contains(items2[i]));
              System.out.println(items2[i]);
           } else {
               System.out.println("Elements are not equals");
           }
        }
    }

In String [] items1 I want to have these values shuffled and the method for this is right here

public String[] getRandomWayStringValues(String [] list){

    String[] newValuesList = new String[] {"PC","Desktop","Sistem"};
    List<String> strList = Arrays.asList(newValuesList);
    Collections.shuffle(strList);
    return newValuesList = strList.toArray(new String[strList.size()]);

}

My method works when the title contains the word "Sistem" but if I have a title with "Desktop" or "PC" it doesn't work and I think that the getRandom method doesn't do what I want to do

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    Possible duplicate of [Test if a string contains any of the strings from an array](https://stackoverflow.com/questions/8992100/test-if-a-string-contains-any-of-the-strings-from-an-array) – Gaskoin Dec 05 '17 at 22:01

1 Answers1

0

First of all you should have always the same value in items1 and items2 because you do something like:

String[] items2 = titles.split(",");
String[] items1 = getRandomWayStringValues(items2);  // this is replaced further
items1 = titles.split(",");   // this will replace items1 value

You can think about such solution:

List<String> possibleTitles = Arrays.asList("Sistem", "PC", "Desktop");
...
String title = "You title here with PC or whatever";

for(String possibleTitle : possibleTitles) {
    if(title.contains(possibleTitle)) {
        assertTrue(driver.getTitle().contains(possibleTitle));
    }
}

fail();
Gaskoin
  • 2,469
  • 13
  • 22
  • Before this step, I made a random selection for a random product displayed on the page I don't know how to check the title page for a random product that is why I made this String with these three values "Sistem","PC","Desktop" – Alex Dorha Dec 05 '17 at 22:12
  • @AlexDorha you might be missing what the first part of this answer is saying. Both `items2` and `items1` are being set to be `titles.split(",");`. Your looping doesn't use `getRandomWayStringValues()` because you immediately replace it with `items1 = titles.split(",")`. I think most of your problems with this would be solved if you stepped through in a debugger and looked at what all of your values are as you step through the logic. – mrfreester Dec 05 '17 at 22:52