2010-08-06 5 views
0

Juste en utilisant du sélénium pour remplir certaines formes pour moi. J'en ai besoin pour générer une valeur unique pour mon champ de nom d'utilisateur. Comment puis je faire ça?Selenium: Obtenez une valeur unique?

J'ai

Commande: Type
cible: id_of_my_field
Valeur: nom d'utilisateur + unique_value ???

Répondre

5

Vous pouvez utiliser javascript pour faire:

Valeur: javascript { 'nom d'utilisateur' + Math.floor (Math.random() * 100000)}

Ceci ajoutera un nombre aléatoire de 6 chiffres à votre nom d'utilisateur.

Voir this SO question and answers pour plus de détails et d'autres façons de le faire ...

+0

darn.J'essayais 'javascript:'. Je ne connaissais pas la syntaxe '{}'. Merci! – mpen

1

Ma solution qui fonctionne bien pour moi:

Enregistrez le texte suivant dans un fichier .js et ajouter à la Options-> options de la liste « sélénium de base Extensions » ...

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); 
} 

une fois fait, vous pouvez simplement sélectionner la commande TypeRandomName dans sélénium pour chaque zone de texte où vous voulez un « nom » aléatoire à générer.

0

est ici un moyen de générer des numéros uniques sans utiliser JavaScript dans votre code: (i utilisé 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 */ 

Cette méthode donnera nombre vraiment unique qui ne sera jamais se répéter, car il utilise votre temps en cours ...

Vous pouvez ajouter encore plus aléatoire si inclure secondes milles dans DateFormat

+0

Cela échouera dans les scénarios où les nombres sont générés dos-à-dos. Même en incluant plus de détails (fractions de secondes) est susceptible d'échouer en raison du manque de précision. –

1

étapes pour une solution globalement réutilisable est comme suit

1) Télécharger sideflow.js de Download here

2) Ajouter les lignes suivantes en elle:

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 <a href="#locators">element locator</a> 
    */ 

    // 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) Enregistrez le fichier

4) Aller à Sélénium -> Options ure -> Options -> Selenium Core extensions -> donnez la référence de votre fichier là.

5) Maintenant, votre fonction de nom aléatoire apparaîtra dans auto-intellisense et sera comme catégorie de commande "typerandomname".

6) Exemple d'utilisation pourrait être (si l'URL de base est 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 vous aide