Search code examples
webdriverjmeterjmeter-plugins

How to verify Textbox is visible on page using webdriver jmeter script?


Test scenario :

If capctha text box is present(visible) then enter some value.(NOTE :- you can only see captcha Text box if request is coming from same location)

captcha div only going show up if request coming from same location.(Till then captcha div is not even present in a HTML Code)

HTML code :-

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<div class="captcha captcha-container clearfix" id="captcha"><div class="captcha-image"></div><div class="captcha-inputs clearfix"><div class="textInput captcha captcha-container" id="captchadiv"><div class="fieldWrapper"><label for="captchaCode" class="fieldLabel">Enter code</label><input id="captchaCode" name="captcha" type="text" class="hasHelp  validateEmpty  " value="" autocomplete="off" placeholder="Enter code"></div><div class="errorMessage" id="captchaErrorMessage"><p class="emptyError hide">Required</p></div></div><div class="refresh"><a href="" class="captchaRefresh buttonLight onboardingSpritePseudo scTrack:login-click-reload-captcha imageLink"></a></div></div></div>
</body>
</html>

Code :- i am using below login but got NoSuchelement found error

var isEleDis = WDS.browser.findElement(pkg.By.id('captchaCode'))
if(isEleDis.isDisplayed()){
        isEleDis.click()
        isEleDis.sendKeys(['captcha'])
    }

Error :- NoSuchelementfound. (Because id=captchacode not present in a html yet)


Solution

  • WebDriver Sampler doesn't provide a JavaScript API, it just calls Selenium Java libraries methods from JavaScript language so WebElement is not a promise, it is a Java WebElement hence you cannot call .then, you need to redesign your method like:

    var textField = WDS.browser.findElement(pkg.By.id('captCode'))
    
    if (textField.isDisplayed()) {      
        textField.click()
        textField.sendKeys(['hello'])        
    }
    

    More information: