Tuesday, 18 August 2015

Selenium IDE: Extracting a number or value/text from a string or URL and store it in a variable.

This post will basically explain about how we can extract a number say an Object ID or Object Name from a string or absolute URL.

1. Extract object ID from an URL.

Let say we have a URL - https://www.testfoo.org/folder1/folderA1/?objectID=1251

Now from the Url - How we can extract the object ID i.e. 1251 and store it in a variable which we can call or use the same variable (ID) wherever required in our IDE script.

1. store | https://www.testfoo.org/folder1/folderA1/?objectID=1251 | string 
2. store | 1 | delimiter 
3. store | javascript{storedVars['string'].split('?objectID=')[storedVars['delimiter']]} | result 
4. echo | ${result}

Step 1: Store the URL i.e. "https://www.testfoo.org/folder1/folderA1/?objectID=1251" in a variable - "string".
Step 3: Javascript will extract or split the stored variable (string) after "?objectID=" and store the value (ID) in a new variable i.e. result
Step 4: Print the result. The result will be - 1251

HTML Code format:

<tr>
<tr>
    <td>store</td>
    <td>https://www.testfoo.org/folder1/folderA1/?objectID=1251</td>
    <td>string</td>
</tr>
<tr>
    <td>store</td>
    <td>1</td>
    <td>delimiter</td>
</tr>
<tr>
    <td>store</td>
    <td>javascript{storedVars['string'].split('?objectID=')[storedVars['delimiter']]}</td>
    <td>result</td>
</tr>
<tr>
    <td>echo</td>
    <td>${result}</td>
</tr>

OR

1. storeLocation | string
2. store | 1 | delimiter
3. store | javascript{storedVars['string'].split('?objectID=')[storedVars['delimiter']]} | result
4. echo | ${result}

Step 1: Store the current URL location i.e. "https://www.testfoo.org/folder1/folderA1/?objectID=1251" in a variable - "string".
Step 3: Javascript will extract or split the stored variable (string) after "?objectID=" and store the value (ID) in a new variable i.e. result
Step 4: Print the result. The result will be - 1251

HTML Code format:

<tr>
<tr>
    <td>storeLocation</td>
    <td>string</td>
</tr>
<tr>
    <td>store</td>
    <td>1</td>
    <td>delimiter</td>
</tr>
<tr>
    <td>store</td>
    <td>javascript{storedVars['string'].split('?objectID=')[storedVars['delimiter']]}</td>
    <td>result</td>
</tr>
<tr>
    <td>echo</td>
    <td>${result}</td>
</tr>

2. Extract part of a text say Object name using selenium IDE and store it into a variable.

Let say we have a string inside a div (HTML content). For Example; 

<div class="Title">Edit XXX Centre 1-457898, India</div>

And here we want to extract amd store "XXX Centre" in a variable. How can we do this through IDE ?

1. store | Edit XXX Centre 1-457898, India | Text
2. store | 1   | delimiter1
3. store | javascript{storedVars['text'].split('Edit ')[storedVars['delimiter1']]}|string1
4. echo  | ${string1}
5. store | 0 |  delimiter2
6. store | javascript{storedVars['string1'].split(' 1-457898')[storedVars['delimiter2']]} | string2
7. echo  | ${string1}

Result will be: XXX Centre

Step 1: Store the entire string i.e. "Edit XXX Centre 1-457898, India" in a variable - "Text".
Step 3: Javascript will extract or split the stored variable (Text) after "Edit " and store the rest content/string in a new variable i.e. string1
Step 4: Print the result. The result will be - XXX Centre 1-457898, India
Step 6: Javascript will extract or split the stored variable [string1 i.e. XXX Centre 1-457898, India] before " 1-457898" and store the rest content/string in a new variable 
i.e. string2
Step 7: Print the result. The result will be - XXX Centre

HTML Code format:

<tr>
<tr>
    <td>store</td>
    <td>Edit XXX Centre 1-457898, India</td>
    <td>Text</td>
</tr>
<tr>
    <td>store</td>
    <td>1</td>
    <td>delimiter1</td>
</tr>
<tr>
    <td>store</td>
    <td>javascript{storedVars['text'].split('Edit ')[storedVars['delimiter1']]}</td>
    <td>string1</td>
</tr>
<tr>
    <td>echo</td>
    <td>${string1}</td>
</tr>
<tr>
    <td>store</td>
    <td>0</td>
    <td>delimiter2</td>
</tr>
<tr>
    <td>store</td>
    <td>javascript{storedVars['string1'].split(' 1-457898')[storedVars['delimiter2']]}</td>
    <td>string2</td>
</tr>
<tr>
    <td>echo</td>
    <td>${string2}</td>
</tr>
<tr>

Thanks,
Pinaki Mohapatra

No comments:

Post a Comment