使用Selenium设置JavaScript对象属性

时间:2016-09-09 07:25:13

标签: javascript selenium selenium-webdriver javascript-objects

我有这个JavaScript对象,我想用Selenium设置。

var demoPlayResponse = {
 "winningPrizeIndex" : 1,
 "playMode"          : "NORMAL",
 "customerId"        : "pli",
 "gameVersion"       : "0-1-86",
 "gameId"            : "blue250k",
 "seed"              : 99
};
mws.GameModel.setPlayResponse(demoPlayResponse);
mws.GameModel.setGameMode(demoPlayResponse.playMode);

有没有办法做到这一点?

我试过了:

public void testMethod()
    {
        driver = BrowserFactory.getDriver("chrome", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");

        driver.get("http://jenkins.mws.com");

        WebDriverWait waitPage = new WebDriverWait(driver, 20);

        ((JavascriptExecutor) driver).executeScript("return getDemoPlayResponse()");
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Webdriver客户端的execute() method,以便在浏览器中进行分配。它有点hacky,但我认为它会像这样:

client.execute(function() {
  var demoPlayResponse = {
   "winningPrizeIndex" : 1,
   "playMode"          : "NORMAL",
   "customerId"        : "pli",
   "gameVersion"       : "0-1-86",
   "gameId"            : "blue250k",
   "seed"              : 99
  };
  mws.GameModel.setPlayResponse(demoPlayResponse);
  mws.GameModel.setGameMode(demoPlayResponse.playMode);
});