在jmeter webdriver中传递标题/ Cookie

时间:2016-06-30 19:37:28

标签: cookies selenium-webdriver header jmeter

尝试将硬编码标头传递到jmeter webdriver脚本

var pkg=JavaImporter(org.openqa.selenium,org.openqa.selenium.support.ui) 
var Thr=JavaImporter(java.lang.Thread)
var wait = new pkg.WebDriverWait(WDS.browser, 10) 
//var ui=JavaImporter(org.openqa.selenium.support.ui) 
WDS.sampleResult.sampleStart() 
var usernameField = WDS.browser.findElement(pkg.By.id('uname')) 
usernameField.sendKeys(['TDM_73312'])
var passwordField = WDS.browser.findElement(pkg.By.id('pw_TLNPI')) 
passwordField.sendKeys(['abcd'])     
WDS.sampleResult.setRequestHeaders('C1_PID:S6VwSlA5ncV0XCUAgwXXrh399zhOZRM9xuIgueFxe2M48GKvRtN2Kd4Dg3zixa0r'+'\n'+'profile_ref_id:S6VwSlA5ncV0XCUAgwXXrh399zhOZRM9xuIgueFxe2M48GKvRtN2Kd4Dg3zixa0r')
Thr.Thread.sleep(2000)
var loginButton=WDS.browser.findElement(pkg.By.id('login-start-button')) 
loginButton.click() //click on the login button
WDS.log.info(WDS.name + ' has logged an entry')
WDS.sampleResult.sampleEnd()

我需要传递这两个标题才能成功登录

WDS.sampleResult.setRequestHeaders('C1_PID:S6VwSlA5ncV0XCUAgwXXrh399zhOZRM9xuIgueFxe2M48GKvRtN2Kd4Dg3zixa0r'+'\n'+'profile_ref_id:S6VwSlA5ncV0XCUAgwXXrh399zhOZRM9xuIgueFxe2M48GKvRtN2Kd4Dg3zixa0r')

当我运行此脚本时,它未成功登录

1 个答案:

答案 0 :(得分:2)

  1. 很遗憾,您无法将HTTP标头修改为it is not possible by WebDriver design
  2. 为了操作Cookie,您可以使用WebDriver.manage().addCookie()方法。给定JavaScript(WebDriver采样器的默认语言),可以这样做:

    var myCookie = new org.openqa.selenium.Cookie("Cookie_Name","Cookie_Value");
    WDS.browser.manage().addCookie(myCookie);
    
  3. 有关更多WebDriver采样器提示和技巧

    ,请参阅The WebDriver Sampler: Your Top 10 Questions Answered文章