Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Coding problem with webking.api.browser2.WebBrowser

Options
LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited April 2009 in Ask a question
Performs next action without completely populating the textarea
Hi,

I've recorded a path and generated JUnit testcases using WebKing. Now I'm modifying these JUnit testcases using the WebKing API.

Problem Descripotion
On my webpage there is a textarea and a Save button. My requirement is to populate this textarea with the text of length 1000 to 2000 chars and then once it is fully populated then click on the Save button. The problem is that the code for "Save button click" executes before the textarea is fully populated. This results in the incomplete data in the textarea and hence in the database. This problem is because of the large size of the data takes time to populate in the textarea and I've not been able to find out any method in the webking.api.browser2.WebBrowser that provides the "wait" functionality on the textarea update.

Please see the below code extract.

StringBuffer sb = new StringBuffer(); // it stores 1000 to 2000 characters
ElementLocator locator = new DescriptiveElementLocator("textarea", "name", "content", 0);
browser.waitForElementPresent(locator);
browser.type(locator, sb.toString());

locator = new DescriptiveElementLocator("input", "value", "Save", 0);
browser.waitForElementPresent(locator);
browser.clickAndWaitForPageLoad(locator);


Putting following two lines after browser.type(locator, sb.toString()) also doesn't help.

browser.waitForAsyncRequestsToFinish();
browser.waitForIntervalWithoutTraffic(2500);

Please can anyone help?

Thanks,
Anurag

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited April 2009
    Options
    Hi Anurag,

    The problem is that the "User Action" timeout is too short to allow all the characters to finish typing. (This setting is visible under the Preferences: Browser Testing: User Action Timeout.) This is the maximum amount of time allowed for an action to complete.

    In order to modify the User Action Timeout dynamically from your script, use the following code. Please note that this API is undocumented and is subject to change. This code assumes you are using WebKing 6.0.x.

    CODE
    // override the default user action wait time
    webtool.app.BrowserTestingPreferences.DEFAULT_USER_ACTION_RESPONSE_WAIT_TIME = 60*1000;
            
    browser = new WebBrowser(webkingInstallationDir, browserType, fireFoxExecutablePath);

    This code increases the User Action timeout to 60 seconds. You will need to modify this time to an appropriate length for your testing. This is a maximum time, so you can always err on the side of extra time.

    [edit] Changed the sample code. Also adding:
    In WebKing 6.0.x it is only possible to set the User Action Timeout before the WebBrowser is instantiated. From that point on, that instance of the WebBrowser will use the User Action Timeout defined before the object was created. If you want to limit the scope that this User Action Timeout is applied, you can create a different instance of the WebBrowser object specifically for the tests that require an extended User Action Timeout.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    edited April 2009
    Options
    If you are using SOAtest 6.x, you can modify the user action wait time specifically at the point of typing as shown:

    CODE
    // get the preferences
    com.parasoft.preferences.BrowserTestingPreferenceProvider prefs =
       com.parasoft.preferences.AppPreferenceProvider.
           getBrowserTestingPreferenceProvider();
            
    // get the old default user action wait time
    int oldUserActionTime = prefs.getDefaultUserActionResponseWaitTime();
            
    // set the new user action wait time (in milliseconds)
    prefs.setDefaultUserActionResponseWaitTime(60*1000);
            
    // type the long text
    browser.type(locator, buffer.toString());

    // reset the wait time
    prefs.setDefaultUserActionResponseWaitTime(oldUserActionTime);

    [edit] Modified the code and description
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    The following line throws NullPointerException, because WebtoolApp.getWebtoolAppPreferences() returns NULL.

    webtool.app.BrowserTestingPreferences prefs =
    webtool.app.WebtoolApp.getWebtoolAppPreferences().getBrowserTestingPreferences()


    I don't think I created an SOA Test. I created a very basic Ajax test suite and generated the JUnit testcases, then I tried to modify these testcases as I mentioned in my previous post.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options

    The following line throws NullPointerException, because WebtoolApp.getWebtoolAppPreferences() returns NULL.

    You're right--I made a false assumption when I made my post earlier. I have corrected the code in the post above. Please give this a try.

    Also please see the additional note:

    In WebKing 6.0.x it is only possible to set the User Action Timeout before the WebBrowser is instantiated. From that point on, that instance of the WebBrowser will use the User Action Timeout defined before the object was created. If you want to limit the scope that this User Action Timeout is applied, you can create a different instance of the WebBrowser object specifically for the tests that require an extended User Action Timeout.

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    Hi,

    Thanks for your advice. This problem got resolved with the following line before creating the WebBrowser instance in setup() method of WebKing generated JUnit file.

    webtool.app.BrowserTestingPreferences.DEFAULT_USER_ACTION_RESPONSE_WAIT_TIME = 60*1000;

    But, since the data is being fetched from the database to populate in the textarea so it may not 'always' be between 1000 to 2000 chars. It can be much larger too. In that case the default_user_action_response_wait_time = 60 seconds may not be enough to populate the textarea.

    So to keep the variable length of the content in mind, I thought of the following workaround. I know this is not a good way, but it works! at least in the scenario where we are not mainly concerned about the performance of the JUnit test cases.

    StringBuffer sb = new StringBuffer();
    .....
    .....
    while(ResultSet.next())
    {
    sb.append(<append the value from database>);
    }//after this loop's execution the length of sb can be anything
    .....
    .....

    locator = new DescriptiveElementLocator("textarea", "name", "content", 0);
    browser.waitForElementPresent(locator);
    browser.type(locator, sb.toString());

    String textAreaValue = browser.extractValue("markupTextarea", "text"); //"markupTextarea" is the 'id' of this textarea
    int tempLength = textAreaValue.length();
    int bufferLength = sb.toString().length();
    while (tempLength < (bufferLength))
    {
    textAreaValue = browser.extractValue("markupTextarea", "text");
    tempLength = textAreaValue.length();
    }


    So in short, browser.type() starts the typing of data in the textarea and then we continuosly check if all the data is typed before proceeding to click on Save button.

    Thanks,
    Anurag
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    Hey Anurag,

    That is a great solution! Thank you for sharing it here on the boards