0

I want to verify if a pop-up appears, so I went to inspect element of the link and I tried to match the ownerID from the element to that of the url that pops-up but the issue that I have is that when I inspect element, the element string is messy and it's not easy to extract the ownerID from it.

enter image description here


I am trying to get the ownerID value, which is highlighted in yellow, then I want to match this ownerID to the pop-up's URL ownerID.


If there are more efficient ways to test this other than the way I mentioned please let me know.

  • Can you show us what you have already tried? – Julian Aug 14 '15 at 17:55
  • @JulianCleary not sure if I can show what I have tried since I am not sure how to extract the ownerID value which was the point of the question? I already got the page url, the issue I am having is getting the ownerID value which is highlighted in yellow. – Robben Aug 14 '15 at 18:00
2

Unfortunately, I don't think there is enough information to make any alternate suggestions. It would be better if the popup has some visible text that's unique enough then you can use it for identification. Also, when you say popup, do you mean

  • A dialog box? Or,
  • An alert? Or,
  • A lightbox component?

This is one way in which you can get to that ownerId specifically:

It looks like the element's attribute data-atcui-config holds a JSON object as it's data.

Based on the HTML that you posted, you could get a WebElement instance to that node with something along the lines of:

// This is where we will put the final value.
int ownerId;

// Lets get the web element first.
WebElement myElement = 
    webDriver.findElement(By.cssSelector("span[span-atcui-role='popup']"));

// Now that we have the web element, we'll instantiate a JSONObject for easy parsing.
JSONObject json = new JSONObject(myElement.getAttribute("span-atcui-config"));

// Now lets get the raw url out of that big JSON string.
String url = json.getString("url"); // Get the url


// From here we're just parsing the url as a normal string, this can be done many ways.
Pattern pattern = Pattern.compile("^.*?(?:\\?|&)ownerId=(\\d+)(?:&|$).*$");
Matcher matcher = pattern.matcher(url);

if (matcher.find()) {
    ownerId = Integer.parseInt(matcher.group(1)); // Get the number from the first group
}

Few notes about this solution:

  • I didn't run this through a compiler, so it probably won't work as it is.
  • Even if it does compile, my regex is a bit fuzzy and that particular pattern is one I found online, so that may not do what quite I think it does...

What is basically happening here is:

We target the html node we care about by using a CSS selector that targets only that node and getting an instance of the WebElement. Once we have the WebElement, we want to get the value of it's span-atcui-config attribute.

Since we can determine that the attribute's value is JSON, we can instantiate a new JSON Object that we'll use to parse it. The JSON object will let us easily get the URL by calling getString(String key) to get it's value.

Now we have the URL, and we know that the URL has a parameter called ownerId and has some integer value. So this is where my potentially shoddy regex comes into play, we use a negative look ahead to get the first found series of numbers that come after an instance of 'ownerId'.

Once we have our pattern and matching objects instantiated with our Regex and URL string. We can check to see if it finds our pattern, and then get it as the first group.

Edit: A better way to parse the URL String after you get it might be to follow the accepted answer on this question https://stackoverflow.com/questions/10728093/how-can-i-get-this-url-parameter-with-regex. It uses HttpClient, so you'll need to be able to bring in external dependencies in order to use it.

Edit 2: A better Regex might be [&?]ownerId=(\d+) for simplicity. Just match it to result group 1.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.