假设我有一个网站可以同时打开多个窗口。打开窗口的数量是未知的,每个窗口中的文本也是未知的。我该如何测试或确保至少以下字符串(苹果、香蕉、胡萝卜、日期)包含在一组窗口中。
请记住,每页只能有一个字符串,例如:
- 窗口 1 - 胡萝卜
- 窗口 2 - 苹果
- 窗口 5 - 日期
- 窗口 10 - 香蕉
假设我有一个网站可以同时打开多个窗口。打开窗口的数量是未知的,每个窗口中的文本也是未知的。我该如何测试或确保至少以下字符串(苹果、香蕉、胡萝卜、日期)包含在一组窗口中。
请记住,每页只能有一个字符串,例如:
我写了一个小小提琴来展示你如何能够做到这一点。我依赖 jQuery 和浏览器 localStorage,因此该解决方案不适用于古老的浏览器。将代码粘贴到您的
$(document).ready
并测试
localStorage.allHaves
位域以查看每个窗口周围还有谁(代码可以共享)。记得做一个
parseInt
获取后就localStorage.allHaves 值!
假设你的五个浏览器有五个地址。您可以使用 , 获取页面内容URLConnection
,而不是在该内容上搜索特定单词。使用浏览器的地址循环以下代码。
String[] strings = {"apple", "banana", "carrot", "date"};
URL oracle = new URL("http://www.oracle.com/");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
StringBuilder output = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
output.append(inputLine);
}
for (String token : strings) {
if(output.indexOf(token) != -1){
System.out.println(token +" is found at "+ output.indexOf(token));
}
}
一种使用 jquery 的快速解决方案,向您展示您需要的所有主要功能,
JS
$(document).ready(function () {
var searchForWords = [['Carrot',false],['Apple',false],['Date',false],['Banana',false]]
var windows=[];
windows.push(openWindow(1));
windows.push(openWindow(2));
windows.push(openWindow(3));
windows.push(openWindow(4));
windows.forEach(function(w){
searchForWords.forEach(function(word){
if($(w.document).find('body').html().indexOf(word[0])!=-1){
console.log('found word'+word[0]);
word[1]=true;
}
});
});
var allFound = true;
searchForWords.forEach(function(word){
if(!word[1]){
allFound=false;
return;
}
});
if(allFound){
alert('all words found');
}else{
alert('did not find all words!');
}
});
function openWindow(wId) {
var w = window.open();
w.document.write('<html><head><title>test</title>');
w.document.write($("#test" + wId).html());
w.document.write('</body></html>');
w.document.close();
return w;
}
HTML
<input type="button" value="check content"></input><!-- this button is used in 2nd version, http://jsfiddle.net/xtxK9/1/ -->
<div id="test1" class="test-content">this is content for the test window1 Carrot</div>
<div id="test2" class="test-content">this is content for the test window2 Apple</div>
<div id="test3" class="test-content">this is content for the test window3 Date</div>
<div id="test4" class="test-content">this is content for the test window4 Banana</div>
CSS
.test-content {
display:none;
}
单击按钮时调用“checkContent”函数的另一个版本可以在这里找到,
JS
$(document).ready(function () {
var searchForWords = [['Carrot',false],['Apple',false],['Date',false],['Banana',false]]
var windows=[];
windows.push(openWindow(1));
windows.push(openWindow(2));
windows.push(openWindow(3));
windows.push(openWindow(4));
$('#check-button').click(function(){checkContent(searchForWords,windows);});
});
function checkContent(searchForWords,openWindows){
openWindows.forEach(function(w){
searchForWords.forEach(function(word){
if($(w.document).find('body').html().indexOf(word[0])!=-1){
console.log('found word'+word[0]);
word[1]=true;
}
});
});
var allFound = true;
searchForWords.forEach(function(word){
if(!word[1]){
allFound=false;
return;
}
});
if(allFound){
alert('all words found');
}else{
alert('did not find all words!');
}
};
function openWindow(wId) {
var w = window.open();
w.document.write('<html><head><title>test</title>');
w.document.write($("#test" + wId).html());
w.document.write('</body></html>');
w.document.close();
return w;
}