简体   繁体   中英

How to get an old ID and insert into a new text field

I am working on a scenario, where I create a new user. Since, I will be creating tons of new users for regression, I am assigning a time-stamp to distinguish each user id. For instance, ABC_2015020615215, ABC_2015020615222, etc. The code for creating and sending a user with the time-stamp:

public static String now = "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

// user
public static String user_id = PropertyLoader.loadProperty("USER_ID") + now;

Next scenario: I need to grab the last user added, and insert into a new text field, on a different page. (remaining on the same driver session).

This code creates a new user:

//enter user id in the text field and add a new user
driver.findElement(By.name(PvtConstants.ADD_USER_ID_TEXT_FIELD)).sendKeys(user_id);

This is a new text field, where I need to insert the user that was added in the previous step. I am not able to figure that out. Thanks in advance for the help!

driver.findElement(By.id(PvtConstants.READ_USER_ADVANCED_FILTER_USER_SEARCH_FIELD));

HTML:

<table class="table smallInput dataTable" id="dataTableAdvertisers" ">
<thead>
<tr role="row" style="height: 0px;">
<th class="sorting_asc" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
</tr>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
    <tr class="odd">
    <td class="">
    <a href="getadvertiserdetailsNew.do?advertiserKey=198909">Advertiser_20150204130817</a></td>
    <td class="">---</td>
    <td class="">---</td>
    <td class="">---</td>
    <td class="">---</td>
    </tr><tr class="even">
    <td class="">
    <a href="getadvertiserdetailsNew.do?advertiserKey=198910">Advertiser_20150204130923</a></td>
    <td class="">---</td>
    <td class="">---</td>
    <td class="">---</td><td class="">---</td>
    </tr><tr class="odd">

   </tbody>
   </table>

I will use a Xpath and with passing the user_id as parameter. Note: this xpath matches the any a tag with the text supplied by the user_id variable which in your case is the lastly instantiated variable with timestamp.

By byXpath = By.xpath("//a[.='" + user_id + "']");

WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byXpath));
String newUser  = myDynamicElement.getText().trim();

//send newUser this to the search field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM