我想断言某些页面源代码中是否存在的文本,。
页面源代码:

<script type="text/javascript">

    var rlSerial = '1057388';//Wanted to assert this text
    var rlAnid = 'I888pokerrussiaI';
    var rlFlag = '';
    var rlSearchTerm = '';
    var rlSearchEngine = '';
    var rlCurrency = '';
    var rlLang = 'en';
    var rlDl =  '';
    var rlCampaignId = '';
    var rlCountry = 'ind';
    var rlServerTime = '2015-08-10 10:37:53';
    var rlPid = '';
    var rlMkw = '';
    var MGbannerTag = '';
    var rlWtc  = '';
    var rlWtv  = '';

</script>

我使用了以下脚本,但是它不起作用
Webdriver脚本:
driver.findElement(By.xpath("//*[@id='menu-item-154']/a")).click();
String Tournament=driver.findElement(By.xpath("//*[@id='text-31']/div/a/img")).getAttribute("outerHTML");
String VTournament=driver.findElement(By.xpath("//*[@id='tertiary']/div/div/div/p/a/img")).getAttribute("outerHTML");
Tournament.contains(Default_BannerName);
VTournament.contains(Default_VBannerName);
driver.findElement(By.xpath("//*[@id='text-31']/div/a/img")).click();
for (String winHandle : driver.getWindowHandles())
{
    driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
    boolean b = driver.getPageSource().contains("var rlSerial = '1058358';");
    assertTrue(b);
}

请引导我出去

最佳答案

//Only way you can read a variable is using javascript
//with <code>executeScript</code> method as shown bellow.
private void test(){
    String script = "return rlSerial;";
    String value = (String) ((JavascriptExecutor)driver).executeScript(script);
    System.out.println(value);
    //Use assert here
}

09-27 16:16