本文介绍了使用Selenium Webdriver测试sessionStorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写基于Java的selenium-webdriver测试.我正在测试的应用在storageSession中设置了某些值,例如sessionStorage.setItem("demo", "test")
,如何在测试中检查并断言存储的变量demo
的值
I'm writing Java based selenium-webdriver tests. The app that I'm testing sets certain values in storageSession e.g. sessionStorage.setItem("demo", "test")
, how can I check and assert the value of the stored variable demo
inside my test
推荐答案
找到了一个很棒的实用程序类 https://gist.github.com/roydekleijn/5073579
Found a great utility class https://gist.github.com/roydekleijn/5073579
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class LocalStorage {
private JavascriptExecutor js;
public LocalStorage(WebDriver webDriver) {
this.js = (JavascriptExecutor) webDriver;
}
public void removeItemFromLocalStorage(String item) {
js.executeScript(String.format(
"window.localStorage.removeItem('%s');", item));
}
public boolean isItemPresentInLocalStorage(String item) {
return !(js.executeScript(String.format(
"return window.localStorage.getItem('%s');", item)) == null);
}
public String getItemFromLocalStorage(String key) {
return (String) js.executeScript(String.format(
"return window.localStorage.getItem('%s');", key));
}
public String getKeyFromLocalStorage(int key) {
return (String) js.executeScript(String.format(
"return window.localStorage.key('%s');", key));
}
public Long getLocalStorageLength() {
return (Long) js.executeScript("return window.localStorage.length;");
}
public void setItemInLocalStorage(String item, String value) {
js.executeScript(String.format(
"window.localStorage.setItem('%s','%s');", item, value));
}
public void clearLocalStorage() {
js.executeScript(String.format("window.localStorage.clear();"));
}
}
感谢罗伊
这篇关于使用Selenium Webdriver测试sessionStorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!