Recording and storing selenium IDE random values - automation

I need to extract with selenium IDE a random value that generates always after login again into the app. How can I extract the new value when the page is loaded again?
ex:
Logout button:
id=ext-gen123 Logout
That ID is always different when I logout and login again.. So as you all can see, it's kind of weird when I ran the test cases again and again.

Is there part of the string that's always the same?
xpath=//a[matches(#id,'ext-gen.*')]
See if you can consistently select it with that.
then use the storeAttribute command
storeAttribute | xpath=//a[matches(#id,'ext-gen.*')]#id | genIdVarName
You could add a class to the login element to make it easier to select.
Then you can use javascript if you want just part of the id string
some thing like
storeEval | storedVars['genIdVarName'].replace("ext-gen", "") | genNumberVarName
If this isn't what you want please add more detail to your question.

Related

Protractor sendKeys issue with scripted input fields

I'm automating e2e tests with Protractor on an angular app.
However, I have an issue when sending keys on input fields.
The sendKeys would miss few characters everytime so I found a workaround :
static sendKeys(value, element){
value.split('').forEach((c) => element.sendKeys(c));
}
This works well but it takes more than 3 times the time the original sendKeys function would.
Well no problem my tests are still functionnal right ?
My app now has new fields with scripts behind them.
One of them is a datepicker input, you can either choose from the datePicker or type it manually. However, for today's date you would type 09022018 and the slashes are automatically appended at the right place (like so 09/02/2018). If you were to enter a wrong date the field is cleared.
Now back to the problem : it seems that both my implementation of sendKeys and the original one loose focus after each submitted key. This means that I can't enter a valid date in the input field as it's cleared after each simulated keypress.
I could use browser.executeScript to fix it but I wouldn't be able to test the functionnality adding slashes. Also, as you type, the datepicker is still open and refreshes after each keypress, you can select a date from it at any time and that is also a feature I want to test.
Thanks in advance
Use executeScript to set the date in backgrond, then use sendKeys to enter a space or Tab at the end to trigger the Keyborad event which will check the input and format the input with slash
function enterDate(date) {
var script = 'arguments[0].value=arguments[1]';
// input box for date
var dateBox = element(by.xxx(yyy));
browser.executeScript(script, dateBox, date);
dateBox.sendKeys(" ");
// or try send Tab
dateBox.sendKeys(protractor.Key.TAB);
}
enterDate('09022018');
You can try this solution on other fields you fixed but take 3 more time.

How can I store a dynamic text value in selenium ide and use it later in different window

I am new in selenium and trying to learning. I am creating a script and I got stuck here, I want to store a dynamic value from text message on web page ex. " Event Name:Test" this Event Name is dynamic and I want to store this and want to get in other window. In 2nd window i want to use this value(Test) to verify in the page
I tried storeValue, StoreText and storeAttribute command and getting error message for xpath or "element not found".
Please help me and advice me , what should I do?
Can You please suggest me the Xpath for storing and retrieving the event name. Please help me...
Thanks in advance,
Niru
If you are using the same test-case to navigate from page 1 to page 2 you can use the storeText function in the IDE to store the value of your event name. And then you can use the same variable in the page 2 for verification.
For example, in page 1 to store the value of event you use:
storeText(locator1, temp)
And then on page 2 you use assert:
assertText(locator2, ${temp})

In Selenium IDE, can I store an element to use in subsequent asserts?

I've just started using Selenium-IDE (not looked at selenium-RC yet: if somebody tells me that that is the answer to my question I'll look at it)
One of the operations I'm testing generates some output in a table in the next HTML page, but the order of the rows is not predictable.
I can obviously use 'assertTextPresent', but I want to do a bit more, and check that various bits of text are in the same row.
What I would like to be able to do is to identify a tr by some content, and then use that tr in subsequent asserts; something like
storeExpression //table[#id='TABLE_6']/td[.='case_1']/.. row
assertText ${row} 'Some text'
assertText ${row} 'Some other text'
to check that 'Some text' and 'Some other text' occur in the same table row as 'case_1'.
I haven't got this to work so far, and I'm not sure whether it is possible, or what syntax to use if it is.
Has anybody managed to do this?
You can use the assignId command to temporarily assign a value to the element's id attribute. For example:
assignId | //table//td[.='case_1']/.. | myRow
assertText | id=myRow | Some text
assertText | id=myRow | Some other text
you can use xpath=${row}
see Selenium: Is it possible to concatenate an xpath with a variable? (second response)

Clicking on several links part dynamically generated

I'm new in Selenium and testing matters.
I'm trying to capture the id of an hyperlink element that is dynamically part generated.
The click action is recorded like as below when i make click on in selenium, the part in bold are dynamically generated, there are many of them on my page and can differ from one site to another(I'm testing cms). I'd like to capture and click on any one.
This is what I've tried to do since:
storeAttribute | //button#class onclick="setLocation(javascript{baseUrlSelection()}['/checkout/cart/add/uenc/(a-zA-Z0-9)/product/(0-9)]'')" | myid
echo | ${myid} |
clickAndWait | ${myid[0]}
It doesn't work
My links looks like this when click action is recorded in selenium:
buton[#onclick="setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdC9NYWdlbnRvSGls
YWlyZURlbW9WMi9tYWdlbnRvZGVtb0hpbGFpcmVWMi9pbmRleC5waHAvY2F0ZWdvcmllMS5odG1sP19fX1NJRD1V/product/1/')">
Please Help.
Are you using the Selenium IDE? It would probably be much easier to do something using the other development environments, however if you really want to do looping:
Get the looping user-extension from this page
and run a script like this.
store | 0 | myCurrent
storeEval | var pattern=new RegExp("\\w*http://localhost/mydomaine/index.php/checkout/cart/add\\w*");var i=0;var total=0;while(i<window.document.getElementsByTagName('input').length){if (window.document.getElementsByTagName('input')[i].id.match(pattern)){window.document.getElementsByTagName('input')[i].id = 'testID_' + total;total=total+1;}i=i+1;}total; | myTotal
while | storedVars.myCurrent < storedVars.myTotal
storeAttribute | //input[contains(#id,'testID_${myCurrent}')]#class | myid
echo | ${myid}
clickAndWait | //input[contains(#id,'testID_${myCurrent}')]
store | javascript{storedVars.myCurrent++}
endWhile
What does the click on these buttons do? Does it postback the page or anything like that? If so, you'll need to move the storeEval | var pattern.... inside the while loop. What the javascript does is rename all of the inputs on the page that match the RegEx pattern (which you'll probably need to change to match your button ID's) to a sequential ID so you can loop through them easily. If there is a different pattern you can exploit, feel free to do that.
Javascript adapted from here
You can use the xpath contains() function to locate the items:
storeAttribute | //input[contains(#onclick,"setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/")]#class | myid
echo | ${myid}
clickAndWait | //input[contains(#onclick,"setLocation('http://localhost/mydomaine/index.php/checkout/cart/add/uenc/")]#class
It is also a good idea to reference the elements by something other than their onclick attribute. Id or name would be good choices.

Selenium: How do I use javascript to clear a value from a form field?

I'm using the selenium IDE and the Selenium-Fitnesse Bridge fixture and I'm trying to test that when I clear a default value out of a form field, my form displays an error message.
So when I record with the Selenium IDE, what it does is the equivalent of telling Selenium to type nothing.
| type | text_field | |
The problem with this is that the Fitnesse fixture I'm using expects that second argument to not be null.
Is there a way in Selenium to "clear a value" rather than "typing nothing"?
You can do it via javascript as such:
| verifyEval | javascript{this.browserbot.getCurrentWindow().document.getElementById('CONTROL_ID').value = ''} ||
Effectively the verifyEval statement allows you to execute any piece of javascript that you'd like. Makes some difficult problems to accomplish with Selenium much simpler.
I used this tutorial (today believe it or not) to figure things out.
I used this to get it to work. reg_start_date is the id of my input field.
| storeEval | window.document.getElementById('reg_start_date').value = '' |
From the selenium reference:
Note that, by default, the snippet will run in the context of the "selenium" object itself, so this will refer to the Selenium object. Use window to refer to the window of your application, e.g. window.document.getElementById('foo')
Also, avoid wrapping the javascript code with javascript{}, it will not work.
We had this issue at the beginning as well. Try: | type | text_field | blank |
Look for more info on the Fitnesse documentation for the use of blank and null.
:)