'WebDriver Sample with Java error - Encountered "WDS"

I am using java as scripting language in JMeter 3.0 and jmeter-plugins-webdriver-1.4.0.jar. When I run my script which is supposed to open a browser and go to gmail.com, and sign in. I get the following error:

ERROR - com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler: In file: inline evaluation of: ``import org.openqa.selenium; 

import org.openqa.selenium.support.ui;  WDS.sampleRe . . . 

Encountered "WDS" at line 5, column 1.in inline evaluation of: 
import org.openqa.selenium; 

import org.openqa.selenium.support.ui;  

WDS.sampleRe . . . '' at line number 5 

ERROR - com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler: In file: inline evaluation of: 

import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*;  import o . . . '' Encountered "WebElement" at line 9, column 1.
     in inline evaluation of: 

import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*; 

 import o . . .  at line number 9

Here are my 2 scripts :

Open Browser:

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;


WDS.sampleResult.sampleStart()
WDS.browser.get('http://gmail.com')
// login details
WDS.sampleResult.sampleEnd()

LogIn:

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


WDS.sampleResult.sampleStart()


WebElement userIdElement = WDS.browser.findElement(By.id(("username"));
userIdElement.sendKeys('${email}');
WebElement pwdElement = WDS.browser.findElement(By.id("password"));
pwdElement.sendKeys('${pwd}');
WebElement signIn = WDS.browser.findElement(By.id("login"))
signIn.click();
// login details
WDS.sampleResult.sampleEnd()


Solution 1:[1]

There are multiple problems with your test

  1. Missing semicolons
  2. Unbalanced parentheses
  3. Given Java language you cannot access JMeter Variables like ${email}
  4. Invalid WebElement IDs

Here is example fixed code:

  • Open Browser

    import org.openqa.selenium.*;
    import org.openqa.selenium.support.ui.*;
    
    
    WDS.sampleResult.sampleStart();
    WDS.browser.get("http://gmail.com");
    // login details
    WDS.sampleResult.sampleEnd();
    
  • Login

    import org.apache.jmeter.threads.JMeterContextService;
    import org.apache.jmeter.threads.JMeterVariables;
    
    
    import org.openqa.selenium.*;
    import org.openqa.selenium.support.ui.*;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    
    
    WDS.sampleResult.sampleStart();
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    
    
    WebElement userIdElement = WDS.browser.findElement(By.id("Email"));
    userIdElement.sendKeys(new String[]{vars.get("email")});
    WebElement nextButton = WDS.browser.findElement(By.id("next"));
    nextButton.click();
    WebElement pwdElement = WDS.browser.findElement(By.id("Passwd"));
    pwdElement.sendKeys(new String[]{vars.get("pwd")});
    WebElement signIn = WDS.browser.findElement(By.id("signIn"));
    signIn.click();
    // login details
    WDS.sampleResult.sampleEnd()
    

Also consider using Explicit Waits as it is quite unlikely your that your browser loads pages immediately and hence you'll get tons of NoSuchElementExceptions

See The WebDriver Sampler: Your Top 10 Questions Answered guide for more information on using the WebDriver Sampler in JMeter tests.

Solution 2:[2]

I have used this sendKeys method Eg: WDS.browser.findElement(By.id("j_username")).sendKeys(new String[]{vars.get("abcd")});

so, instead of passing abcd, it is passing NULL

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 abhirup