我在Java Selenium测试套件中运行一个方法,该方法隐藏了网页中的动态元素(例如基于位置的电话号码),这样当我稍后执行屏幕截图比较时 - 它就赢了因为这些而失败。
我试图减少此方法中重复的代码段。我使用findElements,每次都返回一个元素的所有实例,因此我可以循环使用#E;通过每一个隐藏它。我每次都会调用相同的JavaScript命令 - 所以我想也许有一种方法可以做得更整齐 - 但我很难这样做。更改的位是元素的变量名称以及通过Selenium查找它的方法。希望这是有道理的!
我的代码是:
private void hideDynamicElements() throws IOException, InterruptedException {
List<WebElement> dynamicTelephoneNumbers = driver.findElements(By.className("telephone-number"));
for (WebElement hideElement : dynamicTelephoneNumbers){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
List<WebElement> dynamicChatWindow = driver.findElements(By.cssSelector("#chatContainer"));
for (WebElement hideElement : dynamicChatWindow){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
List<WebElement> dynamicDemoChat = driver.findElements(By.cssSelector("#demoFormChat"));
for (WebElement hideElement : dynamicDemoChat){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
List<WebElement> dynamicDemoChatCTA = driver.findElements(By.className("reach-out"));
for (WebElement hideElement : dynamicDemoChatCTA){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
List<WebElement> dynamicBlogHeader = driver.findElements(By.cssSelector("div.cf.article-header"));
for (WebElement hideElement : dynamicBlogHeader){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
List<WebElement> dynamicBlogFeed = driver.findElements(By.className("js-blog-feed-container"));
for (WebElement hideElement : dynamicBlogFeed){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
TimeUnit.SECONDS.sleep(1); //Wait to be sure all elements have disappeared in the browser
}
答案 0 :(得分:2)
创建一个类似的函数: -
void visiblity(String Classname, Element hideElement)
{
List<WebElement> dynamicBlogFeed = driver.findElements(By.className(Classname));
for (WebElement hideElement : dynamicBlogFeed){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
无论何时需要,都可以反复调用此功能。
希望它会对你有所帮助:)。
答案 1 :(得分:1)
减少代码的一个好方法是确定方法中的职责,例如:
然后,按照目的模块化代码:
private void hideDynamicElements() throws IOException, InterruptedException {
List<By> criteria = Arrays.asList(
By.className("telephone-number"),
By.cssSelector("#chatContainer"),
By.cssSelector("#demoFormChat"),
By.className("reach-out"),
By.cssSelector("div.cf.article-header"),
By.className("js-blog-feed-container")
);
for(By crit : criteria) {
List<WebElement> elements = driver.findElements(crit);
hideElements(elements);
}
}
private void hideElements(List<WebElement> elements) {
for (WebElement hideElement : elements){
((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility='hidden'", hideElement);
}
}
如果您希望以下情况,此方法还可以最大限度地减少代码中的更改: - 改变隐藏元素的方式,或 - 隐藏其他类下的元素
答案 2 :(得分:0)
首先,按如下方式初始化ArrayList:
INNER JOIN tag_list
ON client.id = tag_list.id
AND tag_list.tag IN ("tag1","tag2","tag3")
GROUP BY client.id
HAVING COUNT(DICTINCT tag_list.tag)=3
然后遍历外部循环中的dynamicElements;在内循环中,你将完成你的实际任务。
ArrayList<By> dynamicElements = new ArrayList<By>(Arrays.asList((By.className("telephone-number"), By.cssSelector("#demoFormChat"),...,))
希望它会有所帮助。
答案 3 :(得分:0)
我会使用单个CSS选择器获取所有目标元素,并使用单个JavaScript调用隐藏它们。它更容易维护和更快:
private void hideDynamicElements() throws IOException, InterruptedException {
String selectors = ".telephone-number, #chatContainer, #demoFormChat, .reach-out, div.cf.article-header, .js-blog-feed-container";
String JS_HIDE_CSS_SELECTORS =
"var hide = function(element){ element.style.visibility = 'hidden' }; " +
"var elements = document.querySelectorAll(arguments[0]); " +
"Array.prototype.forEach.call(elements, hide); " ;
((JavascriptExecutor)driver).executeScript(JS_HIDE_CSS_SELECTORS, selectors);
TimeUnit.SECONDS.sleep(1); //Wait to be sure all elements have disappeared in the browser
}