Selenium: Get unique value? - selenium

Just using selenium to fill out some forms for me. I need it to generate a unique value for my username field. How can I do that?
I've got
Command: type
Target: id_of_my_field
Value: username+unique_value ???

You can use javascript to do that:
Value: javascript{'username'+Math.floor(Math.random()*100000)}
This will append a 6 digit random number to your username.
See this SO question and answers for more details and other ways to do this...

My solution which works well for me:
Save the following TEXT as a .js file and add to the Options->Options "Selenium Core Extensions" list...
Selenium.prototype.doTypeRandomName = function(locator)
{
/**
* Sets the value of an input field to a random "name"
* as though you typed it in.
*/
// All locator-strategies are automatically handled by "findElement"
var element = this.page().findElement(locator);
/* The following block generates a random name 8 characters in length */
var allowedChars = "abcdefghiklmnopqrstuvwxyz";
var stringLength = 8;
var randomstring = '';
for (var i=0; i<stringLength; i++) {
var rnum = Math.floor(Math.random() * allowedChars.length);
randomstring += allowedChars.substring(rnum,rnum+1);
}
// Replace the element text with the new text
this.browserbot.replaceText(element, randomstring);
}
Once done, you can simply select the TypeRandomName command in Selenium for each text box where you want a random "name" to be generated.

Steps for a globally reusable solution is as follows
1) Download sideflow.js from Download here
2) Add following lines into it :
Selenium.prototype.doTypeRandomName = function(locator) {
/**
* Sets the value of an input field to a random email id,
* as though you typed it in.
*
* #param locator an element locator
*/
// All locator-strategies are automatically handled by "findElement"
var element = this.page().findElement(locator);
/* The following block generates a random email string */
var allowedChars = "abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var stringLength = 8;
var randomstring = '';
for (var i=0; i<stringLength; i++) {
var rnum = Math.floor(Math.random() * allowedChars.length);
randomstring += allowedChars.substring(rnum,rnum+1);
}
// Replace the element text with the new text
this.browserbot.replaceText(element, randomstring);
};
3) Save the file
4) Go to Selenium ide -> options -> options ->Selenium Core extensions -> give reference of your file there.
5) Now your randomname function will appear in auto-intellisense and will be as "typerandomname" command category.
6) Sample usage could be (if base url is google.com)
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>typerandomname</td>
<td>css=input[class='gbqfif']</td>
<td></td>
</tr>
Hope this helps you

Here is a way to generate unique numbers without using JavaScript in your code: (i used Java)
DateFormat dateFormat = new SimpleDateFormat("ddHHmmss"); /* Here you create object of the class DateFormat, and SPECIFY THE FORMAT (ddHHmmss). (Don`enter code here`t forget to import class Date(ctrl+shift+o if you are using Eclipse)) */
Date date = new Date(); /* Here you create object of the class Date. (Dont forget to import class Date. (Dont forget to import class Date(ctrl+shift+o if you are using Eclipse)) */
String random_number = dateFormat.format(date); /* Assign your current Date(something like 19184123) (ddHHmmSS) to a local variable(it require a String) */
yourWebDriver().findElemnt(By.cssSelector("input#someId")).sendKeys("test"+random_number+"#tester.com"); /* send keys to your input injecting random number */
Such method will give truly unique number that will never repeat itself, because it use your current time...
You can add even further randomness if include mile seconds into DateFormat

Related

Talend Open Studio 7.1 : tJavaFlex output issue

I'm asking for your help,I'm facing a problem with the tJavaFlex component.
editorName ;ProductName ;end_date_resorption_versions;end_date_supported_versions ;end_date_recommended_versions
EditorA;PN_A;31/03/2017,31/03/2017,31/03/2017,31/03/2017,31/03/2017,31/03/2017;null;null
EditorA;PN_A;30/06/2024;null;30/06/2024
EditorA;PN_A;30/11/2020,30/06/2017;null;null
EditorA;PN_A;null;30/06/2024;null
EditorA;PN_A ;null;null;null
EditorA;PN_A;30/11/2020,30/11/2020;null;null
EditorB;PN_B;18/05/2017,31/03/2017,31/01/20;null;null
EditorB;PN_B;03/06/2024;01/02/2020;30/06/2024
EditorB;PN_B;23/12/2014 ;null;null
EditorB;PN_B;null;01/02/2020;30/06/2020
EditorB;PN_B;null;null;null
EditorB;PN_B;12/12/2012,31/12/2020;null;13/01/2020
As u can see there are list of date (string format) in same column.
What i want to do ==> it's to find the min date for each column (not row)
My approach is this :
By column, store all values in an array
convert ListString to ListDate
Find Date min in each ListDate
I thought the best way to do that was to use the tJavaFlex component.
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
**Start Code :**
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// Array for end_date_resorption_versions
List<String> myStringList_edrv = new ArrayList<>();
List<Date> dates = new ArrayList<>();
// Array for end_date_supported_versions
List<String> myStringList_edsv = new ArrayList<>();
List<Date> dates_edsv = new ArrayList<>();
// Array for end_date_recommended_versions
List<String> myStringList_edrev = new ArrayList<>();
List<Date> dates_edrev = new ArrayList<>();
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
**main code :**
if (row4.end_date_resorption_versions == null ){
row4.end_date_resorption_versions = "31/12/2099";
}
if (row4.end_date_supported_versions == null ){
row4.end_date_supported_versions = "31/12/2099";
}
if (row4.end_date_recommended_versions == null) {
row4.end_date_recommended_versions = "31/12/2099";
}
// populating data :
myStringList_edrv.addAll(Arrays.asList(row4.end_date_resorption_versions.split(",")));
myStringList_edsv.addAll(Arrays.asList(row4.end_date_supported_versions.split(",")));
myStringList_edrev.addAll(Arrays.asList(row4.end_date_recommended_versions.split(",")));
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
**end code :**
// transform ListString to ListDate : end_date_resorption_versions
for (int i = 0 ; i < myStringList_edrv.size(); i++) {
dates.add(sdf.parse(myStringList_edrv.get(i)));
}
// transform ListString to ListDate : end_date_supported_versions
for (int i = 0 ; i < myStringList_edsv.size(); i++) {
dates_edsv.add(sdf.parse(myStringList_edsv.get(i)));
}
// transform ListString to ListDate : end_date_recommended_versions
for (int i = 0 ; i < myStringList_edrev.size(); i++) {
dates_edrev.add(sdf.parse(myStringList_edrev.get(i)));
}
// getMinDate : end_date_resorption_versions
row6.out_date_edrv = sdf.format(Collections.min(dates));
// getMinDate : end_date_supported_versions
row6.out_date_edsv = sdf.format(Collections.min(dates_edsv));
// getMinDate : end_date_recommended_versions
row6.out_date_edrev = sdf.format(Collections.min(dates_edrev));
row6.out_editor_name = row4.editor_name;
row6.out_product_name = row4.product_name;
System.out.println("out_date_edrv => " + row6.out_date_edrv);
System.out.println("out_date_edsv => " + row6.out_date_edsv);
System.out.println("out_date_edrev => " + row6.out_date_edrev);
all my values are null, while the results present in the system.out.println are good
my job design is :
tPostgresqlInput----row4(Main)----tJavaFlex_1----row6(Main)----tLogRow
some help would really be appreciated.
You don't need all that java code. Here's a solution using only Talend components which has the advantage of being easier to maintain should your requirement change.
I start by normalizing your date columns; if only end_date_resorption_versions can contain a list of dates, then you can skip tNormalize_2 and tNormalize_3 which normalize end_date_supported_versions and end_date_recommended_versions respectively.
the tMap_1 is probably not needed, I use it to convert the "null" literal to null, since I copy pasted your sample data in a file for my test, otherwise the following conversion would fail.
tConvertType_1 then converts the date string to Date type, by checking the option "Auto cast", and setting the schema as follows:
Finally, tAggregateRow_1 will group on the editorName and ProductName columns and get the minimum date from each date column:
No explanation, just a nasty hack : Try to insert a tFlowToIterate before your tJavaFlex and feed the tJavaFlex with an iterate flow instead of a main flow (See pictures below).
Here, for some reason my tJavaFlex only reads null values from the input main flow
But it manages to read the exact same data when provided through an iterate flow.
Note that as long as your input data flow isn't renamed (row4 in my example), you won't have to touch the code in your tJavaFlex.
I have absolutely no clue on why nor when tJavaFlex wouldn't cope well with flows of type main, and I find that rather frustrating. I would surely appreciate if a talend guru could give me an explanation !

Restart numeration in multiple tables with docx4j

I need to create a .docx file using docx4j with many tables based on a template.
Tables must have rows with automatic numeration.
After copying table from template numeration continues in consecutive tables, like this:
table 1
List item
List item
table 2
List item
List item
How can I restart numeration for every table to obtain this:
table 1
List item
List item
table 2
List item
List item
I found that there exists NumberingDefinitionPart.restart() method that could be helpful but how can I apply it on each table?
Could you give example with java code?
For each table after the first, you need to create/add a list level override to the numbering definitions part, then use that in your numPr (ie in your "list item").
The method you mentioned does this:
/**
* For the given list numId, restart the numbering on the specified
* level at value val. This is done by creating a new list (ie <w:num>)
* which uses the existing w:abstractNum.
* #param numId
* #param ilvl
* #param val
* #return
*/
public long restart(long numId, long ilvl, long val)
throws InvalidOperationException {
// Find the abstractNumId
// (Ensure maps are initialised)
if (em == null ) {
getEmulator();
}
ListNumberingDefinition existingLnd = instanceListDefinitions.get( Long.toString(numId) );
if (existingLnd==null) {
throw new InvalidOperationException("List " + numId + " does not exist");
}
BigInteger abstractNumIdVal = existingLnd.getNumNode().getAbstractNumId().getVal();
// Generate the new <w:num
long newNumId = instanceListDefinitions.size() + 1;
org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
Num newNum = factory.createNumberingNum();
newNum.setNumId( BigInteger.valueOf(newNumId) );
AbstractNumId abstractNumId = factory.createNumberingNumAbstractNumId();
abstractNumId.setVal(abstractNumIdVal);
newNum.setAbstractNumId(abstractNumId);
LvlOverride lvlOverride = factory.createNumberingNumLvlOverride();
lvlOverride.setIlvl(BigInteger.valueOf(ilvl));
newNum.getLvlOverride().add(lvlOverride);
StartOverride start = factory.createNumberingNumLvlOverrideStartOverride();
start.setVal(BigInteger.valueOf(val));
lvlOverride.setStartOverride(start);
// Add it to the jaxb object and our hashmap
((Numbering)getJaxbElement()).getNum().add(newNum);
ListNumberingDefinition listDef
= new ListNumberingDefinition(newNum, abstractListDefinitions);
instanceListDefinitions.put(listDef.getListNumberId(), listDef);
// Return the new numId
return newNumId;
}
https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/NumberingRestart.java is an example of using it.
In your numPr element in your w:p "list item":
<w:pPr>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="1"/>
</w:numPr>
</w:pPr>
set level (ilvl) to the level you used in the method; set numid to the value the method returns.
As noted in the sample, after the first paragraph using newNumId, it doesn't matter whether subsequent paragraphs use that or the original numId.

Behat Pass a value from a test step

I'm trying to make assertion that the random text entered in one field appears on next page (confirmation)
I do it like this
When I fill in "edit-title" with random value of length "8"
/**
* Fills in form field with specified id|name|label|value with random string
* Example: And I fill in "bwayne" with random value of length "length"
*
* #When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
*/
public function fillFieldWithRandomValue($field, $length)
{
$field = $this->fixStepArgument($field);
$value = $this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field, $value);
}
Than I want to make assertion - something like this:
Then I should see text matching "<RANDOM VALUE ENTERED IN THE PREVIOUS STEP>"
is it possible?
UPDATE:
But how would it look like with setters and getters if i want to use a generateRandomString method multiple times and then get the values of this methods one after another? DO I have to make variables and functions for every test step? like this:
When I fill in "x" with random value of length "8"
And I fill in "y" with random value of length "12"
And I go to other page
Then I should see text matching "VALUE ENTERED TO X"
And I should see text matching "VALUE ENTERED TO Y"
You can create a property and set it in the previous step. And use it in the next one, but assert it if it has value.
Also it would be nice and readable to define that property with proper visibility type
/**
* #var string
*/
private randomString;
/**
* Fills in form field with specified id|name|label|value with random string
* Example: And I fill in "bwayne" with random value of length "length"
*
* #When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
*/
public function fillFieldWithRandomValue($field, $length)
{
$field = $this->fixStepArgument($field);
$this->randomString = $this->generateRandomString($length);
$this->getSession()->getPage()->fillField($field, $this->randomString);
}
/**
*
* #Then /^(?:|I )should see that page contains random generated text$/
*/
public function assertPageContainsRandomGeneratedText()
{
//Assertion from phpunit
$this->assertNotNull($this->randomString);
$this->assertPageContainsText($this->randomString);
}
NOTE: Depending on your behat setup - assertion from phpunit might not work.
Since you will will call the generateRandomString method in multiple places then you should also have a method for getting this value like getRandomString like setters and getters.
My recommendation would be to have a class with related methods that handle all the data and not saving in variable in every place you will use data, generate+save and read from the same place anywhere you need.
Tip: You could be more flexible about the step definition and have a default length for the random string in case one one not provided.
High level example:
class Data
{
public static $data = array();
public static function generateRandomString($length = null, $name = null)
{
if ($name = null) {
$name = 'random';
};
if ($length = null) {
$length = 8;
};
// generate string like $string =
return self::$data[$name] = $string;
}
public static function getString($name = null)
{
if ($name = null) {
$name = 'random';
};
// exception handling
if (array_key_exists($name, self::$data) === false) {
return null;
}
return self::$data[$name];
}
}
In context:
/**
* #Then /^I fill in "x" with random value as (.*?)( and length (\d+))?$/
*/
public function iFillInWithRandomValue($selector, $name, $length = null){
$string = Data::generateRandomString($length, $name);
// fill method
}
/**
* #Then /^I should see text matching "first name"$/
*/
public function iShouldSeeTextMatching($variableName){
$string = Data::getString($variableName);
// assert/check method
}
This is high level example, you might need to do some adjustments.
If you have the validation in the same class then you can also have all these in the same class, meaning generateRandomString and getString in the same class with the steps.

dynamically change a part of the variable path

I know this question has been asked a bunch of times, but none of the answers (or at least what i took away from them) was a help to my particiular problem.
I want to dynamically change a part of the variable path, so i don't have to repeat the same code x-times with just two characters changing.
Here's what i got:
In the beginning of my script, i'm setting the reference to PlayerData scripts, attached to the GameManager object like this:
var P1 : P1_Data;
var P2 : P2_Data;
function Start(){
P1 = GameObject.Find("GameManager").GetComponent.<P1_Data>();
P2 = GameObject.Find("GameManager").GetComponent.<P2_Data>();
}
Later, i want to access these scripts using the currentPlayer variable to dynamically adjust the path:
var currentPlayer : String = "P1"; //this actually happens along with some other stuff in the SwitchPlayers function, i just put it here for better understanding
if (currentPlayer.PlayerEnergy >= value){
// do stuff
}
As i was afraid, i got an error saying, that PlayerEnergy was not a part of UnityEngine.String.
So how do I get unity to read "currentPlayer" as part of the variable path?
Maybe some parse function I haven't found?
Or am I going down an entirely wrong road here?
Cheers
PS: I also tried putting the P1 and P2 variables into an array and access them like this:
if (PlayerData[CurrentPlayerInt].PlayerEnergy >= value){
// do stuff
}
to no success.
First of all,
var currentPlayer : String = "P1"
here P1 is just string, not the previous P1/P2 which are referenced to two scripts. So, if you want, you can change
currentPlayer.PlayerEnergy >= value
to
P1.PlayerEnergy >= value
or,
P2.PlayerEnergy >= value
But if you just want one function for them, like
currentPlayer.PlayerEnergy >= value
Then you have to first set currentPlayer to P1/P2 which I assume you are trying to do. You must have some codes that can verify which player is selected. Then, maybe this can help -
var playerSelected: int = 0;
var currentPlayerEnergy: int = 0;
.....
//Use your codes to verify which player is selected and then,
if (playerSelected == 1) {
currentPlayerEnergy = P1.PlayerEnergy;
} else if (playerSelected == 2) {
currentPlayerEnergy = P2.PlayerEnergy;
}
//Now use your favorite function
if (currentPlayerEnergy >= value) {
//Do stuff
}
As there was no reply providing the answer I needed, I'll share the solution that did the trick for me, provided by a fellow student.
Instead of having the PlayerData scripts pre-written, I generate them using a public class function in a Playermanager script. This generates the Playerdata as attached scripts, saved into an array.
I can then access them through Playermanager.Playerlist[Playernumber].targetvariable.
Which is what I wanted to do, only with the Playerdata being attached to a script instead of a gameobject. And it works great!
Here's the full code of my Playermanager Script:
//initialise max players
public var maxplayers : int = 2;
// Initialise Playerlist
static var Players = new List.<PlayerData>();
function Start () {
for (var i : int = 0; i < maxplayers; i++){
var Player = new PlayerData();
Players.Add(Player);
Players[i].PlayerName = "Player " + i;
}
DontDestroyOnLoad (transform.gameObject);
}
public class PlayerData {
public var PlayerName : String;
public var PlayerEnergy : int = 15;
public var Fleet : List.<GameObject> = new List.<GameObject>();
}
As you see, you can put any type of variable in this class.
I hope this helps some of you who have the same problem.
cheers,
Tux

How to verify character count of text field?

I want to check the number of characters I can insert in a text field, and was thinking of using 'for loop' but it would not help as Selenium tries to insert more than required character the field will not accept but test goes on without any failure, so is there a way to get character count of the text field?
Would this work?
final String myLongString = "Something horrrrribly looooong";
final int longStringLength = myLongString.length();
// assuming driver is a healthy WebDriver instance
WebElement elem = driver.findElement(By.id("myInput"));
elem.sendKeys(myLongString);
// it's possible that you'll first need to lose focus on elem before the next line
int realLength = elem.getValue().length();
assertEquals(longStringLength, realLength);
Using Protractor I captured the actual text in the field and then did a forloop to count each letter.
element(by.css('elementPATH')).getAttribute('value').then(function(words){
//forloop to count each word
var x = 0
for(var i = 0; i < words.length; i++) {
x = x + 1;
};
//check condition
expect(x).toBe(200);
return true;
});
Let me know if this helps.