URL split and using part of the URL as variable in another test case

Hi there.

I am currently hitting a brick wall when trying to do the following.

  1. Create a customer account. After the process completed I can view the overview page and the URL has the account number present within.
  2. Extract the account number and store it in variable.
  3. Use the variable in POST REST API call for further testing.
  4. Also use the variable in another test in order to search for account created.

I have succeeded step 1, and currently have a big problem of completing step 2. I have not proceeded to the step 3 yet, so the focus is on extracting the value from the URL.

Can anybody help by outlining step by step how to achieve the step 2 in the above?

Thank you,
Albert.

Here are a couple ways it could be done:

1.) I would first check to see if the acct number is presented anywhere in the DOM, and extract it from there. Is the number present on the “overview page” anywhere (on the page itself, not in the URL for the page)? If so, you would locate the element and get the value through the WebUI API as usual. I can provide a concrete solution for you if the number exists on the page.

2.) If the above solution isn’t going to work for you, then you can get the URL from the current window as follows:

String url = WebUI.getUrl();

Then you would need to parse the number from that string, probably using the substring() method.

I would highly recommend trying the first approach first, as parsing values from a URL can be a bit fragile/flakey.

The number is indeed present on the page, and I can capture it as a test object, however each time when I run test case and successfully create a customer, once in the overview page, the element that is created has different name (div_) so I cannot even get text from it because element would not be present in the subsequent test case.

Maybe there is some way that I do not about? What exactly did you mean when wrote “you would locate the element and get the value through the WebUI API as usual”?

If you could also shed a bit more light about substring method?

Thank you.

This means what you said above:

Then you would do something like:

WebUI.getText(testObject);

or

WebUI.getAttribute(testObject, "value");

depending on how the HTML looks. I would still recommend doing it this way, the “different name (div_)” is not a problem with a proper xpath. If you provide me with the HTML for the account number and the surrounding elements, I can give you one that will work.

Continuing my above example for the second approach, lets say that the WebUI.getUrl() method returned the following:

http://my-server:my-port/some/page/0123456789

where ‘0123456789’ was the account number. Then you would parse this number as follows:

String accountNumber = url.substring(url.length() - 10);

To extract some part of the url, it could be more accurate to use the URL class, see:
https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

depending where the account number you need to extract is located (in path, parameters …) and further apply to it a matcher (e.g regex) or a split or whatever else
More detailed solutions can be found if you post a concrete example of the url structure.

see also: