带有Java错误的WebDriver示例 - 遇到“WDS”

时间:2016-08-16 17:33:29

标签: selenium webdriver jmeter jmeter-plugins

我在JMeter 3.0和jmeter-plugins-webdriver-1.4.0.jar中使用java作为脚本语言。当我运行我应该打开浏览器并转到gmail.com的脚本并登录时。我收到以下错误:

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

以下是我的2个脚本:

打开浏览器:

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


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

登录:

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()

1 个答案:

答案 0 :(得分:0)

您的测试存在多个问题

  1. 缺少分号
  2. 不平衡的括号
  3. 鉴于Java语言,您无法访问${email}
  4. 等JMeter变量
  5. 无效的WebElement ID
  6. 以下是固定代码示例:

    • 打开浏览器

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

      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()
      

    另请考虑使用Explicit Waits,因为您的浏览器不太可能立即加载页面,因此您将获得大量NoSuchElementExceptions

    有关在JMeter测试中使用WebDriver Sampler的更多信息,请参阅The WebDriver Sampler: Your Top 10 Questions Answered指南。