下面列出了org.jsoup.nodes.Document#getElementsByAttributeValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void allFieldsShouldBePresentInView() throws Exception {
final Document document = getDocument();
final List<ConfigMetadata> metadataList = getMetadataList();
for (ConfigMetadata field : metadataList) {
final String name = field.getKey();
final Elements inputFieldForName = document.getElementsByAttributeValue("ng-model", name);
assertThat(inputFieldForName).describedAs(format("No input/textarea defined for {0}", name)).hasSize(1);
assertThat(inputFieldForName.get(0).attr("ng-class"))
.describedAs(format("ng-class attribute is not defined on {0}", inputFieldForName))
.isEqualTo(format("'{'''is-invalid-input'': GOINPUTNAME[{0}].$error.server'}'", name));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", format("'{'''is-visible'': GOINPUTNAME[{0}].$error.server'}'", name));
assertThat(spanToShowError).hasSize(1);
assertThat(spanToShowError.attr("ng-show")).isEqualTo(format("GOINPUTNAME[{0}].$error.server", name));
assertThat(spanToShowError.text()).isEqualTo(format("'{{'GOINPUTNAME[{0}].$error.server'}}'", name));
}
final Elements inputs = document.select("textarea, input, select");
assertThat(inputs).describedAs("should contains only inputs that defined in ElasticProfile.java").hasSize(metadataList.size());
}
@Test
public void allFieldsShouldBePresentInView() {
String template = Util.readResource("/profile.template.html");
final Document document = Jsoup.parse(template);
for (Metadata field : GetProfileMetadataExecutor.FIELDS) {
if (field.getKey().equals(GetProfileMetadataExecutor.SPECIFIED_USING_POD_CONFIGURATION.getKey()) || field.getKey().equals(GetProfileMetadataExecutor.POD_SPEC_TYPE.getKey()) || field.getKey().equals(GetProfileMetadataExecutor.REMOTE_FILE_TYPE.getKey())) {
continue;
}
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
assertThat(inputFieldForKey, hasSize(1));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", "{'is-visible': GOINPUTNAME[" + field.getKey() + "].$error.server}");
assertThat(spanToShowError, hasSize(1));
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
}
final Elements inputs = document.select("textarea,input[type=text],select,input[type=checkbox]");
assertThat(inputs, hasSize(GetProfileMetadataExecutor.FIELDS.size() - 3)); // do not include SPECIFIED_USING_POD_CONFIGURATION, POD_SPEC_TYPE, REMOTE_FILE_TYPE key
}
@Test
public void allFieldsShouldBePresentInView() {
String template = Util.readResource("/plugin-settings.template.html");
final Document document = Jsoup.parse(template);
for (Metadata field : GetClusterProfileMetadataExecutor.FIELDS) {
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
assertThat(inputFieldForKey, hasSize(1));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-show", "GOINPUTNAME[" + field.getKey() + "].$error.server");
assertThat(spanToShowError, hasSize(1));
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
}
final Elements inputs = document.select("textarea,input[type=text],select,input[type=checkbox]");
assertThat(inputs, hasSize(GetProfileMetadataExecutor.FIELDS.size() - 3)); // do not include SPECIFIED_USING_POD_CONFIGURATION, POD_SPEC_TYPE, REMOTE_FILE_TYPE key
}
@Test
public void allFieldsShouldBePresentInView() {
String template = Util.readResource("/auth-config.template.html");
final Document document = Jsoup.parse(template);
for (ProfileMetadata field : MetadataHelper.getMetadata(GitHubConfiguration.class)) {
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
int elementCount = field.getKey().equalsIgnoreCase("AuthenticateWith") ? 2 : 1;
assertThat(format("Should have only one ng-model for %s", inputFieldForKey), inputFieldForKey, hasSize(elementCount));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", "{'is-visible': GOINPUTNAME[" + field.getKey() + "].$error.server}");
assertThat(spanToShowError, hasSize(1));
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
}
final Elements inputs = document.select("textarea,input,select");
//AuthenticateWith is coming twice as it is radio button
assertThat(inputs, hasSize(MetadataHelper.getMetadata(GitHubConfiguration.class).size() + 1));
}
@Test
public void allFieldsShouldBePresentInView() throws Exception {
String template = Util.readResource("/role-config.template.html");
final Document document = Jsoup.parse(template);
final List<ProfileMetadata> metadataList = MetadataHelper.getMetadata(GitHubRoleConfiguration.class);
for (ProfileMetadata field : metadataList) {
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
assertThat(inputFieldForKey, hasSize(1));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", "{'is-visible': GOINPUTNAME[" + field.getKey() + "].$error.server}");
assertThat(spanToShowError, hasSize(1));
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
}
final Elements inputs = document.select("textarea,input,select");
assertThat("should contains only inputs that defined in GitHubRoleConfiguration.java",inputs, hasSize(metadataList.size()));
}
@Test
public void allFieldsShouldBePresentInView() {
String template = Util.readResource("/profile.template.html");
final Document document = Jsoup.parse(template);
for (Metadata field : GetProfileMetadataExecutor.FIELDS) {
final Elements inputFieldForKey = document.getElementsByAttributeValue("ng-model", field.getKey());
assertThat(inputFieldForKey, hasSize(1));
final Elements spanToShowError = document.getElementsByAttributeValue("ng-class", "{'is-visible': GOINPUTNAME[" + field.getKey() + "].$error.server}");
assertThat(spanToShowError, hasSize(1));
assertThat(spanToShowError.attr("ng-show"), is("GOINPUTNAME[" + field.getKey() + "].$error.server"));
assertThat(spanToShowError.text(), is("{{GOINPUTNAME[" + field.getKey() + "].$error.server}}"));
}
final Elements inputs = document.select("textarea,input,select");
assertThat(inputs, hasSize(GetProfileMetadataExecutor.FIELDS.size()));
}
/**
* 获取用户头像URL
* @param username 用户id
* @return result
*/
public String getHeadImg(String username){
try {
HttpResponse response = hk.execute(Constants.NEW_HEAD_URL + username + "&ie=utf-8&fr=pb&ie=utf-8");
String result = EntityUtils.toString(response.getEntity());
Document doc = Jsoup.parse(result);
Elements link = doc.getElementsByAttributeValue("class", "userinfo_head");
return link.select("img").attr("src");
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
/**
* 获取贴吧首页帖子tid列表
* @param tbName 贴吧id
* @param replyNum 定义标志 根据回复数筛选(回复为0的帖子,抢二楼专用)
* @return 帖子tid 数组
* 帖子链接:https://tieba.baidu.com/p/ + tid
*/
public List<String> getIndexTList(String tbName, Integer replyNum){
List<String> list = new ArrayList<String>();
try {
HttpResponse response = hk.execute(Constants.TIEBA_GET_URL + "/f?kw=" + tbName + "&fr=index");
String result = EntityUtils.toString(response.getEntity());
if(StrKit.notBlank(result) && response.getStatusLine().getStatusCode() == 200){
Document doc_thread = Jsoup.parse(result);
//解析出帖子code块
String tcode = doc_thread.getElementById("pagelet_html_frs-list/pagelet/thread_list")
.html()
.replace("<!--", "")
.replace("-->", "");
//放入新的body解析
Document doc = Jsoup.parseBodyFragment(tcode);
Elements link = doc.getElementsByAttributeValue("class", "j_th_tit "); //帖子链接(获取tid)
Elements data = doc.getElementsByAttributeValueMatching("class", "j_thread_list.* clearfix"); //回复数,是否置顶 data-field
for (int i = 0; i < link.size(); i++) {
Element element = link.get(i);
Integer reply= (Integer) JsonKit.getInfo("reply_num",data.get(i).attr("data-field"));
Object isTop = JsonKit.getInfo("is_top",data.get(i).attr("data-field"));
if(isTop != null && ("1".equals(isTop.toString()) || "true".equals(isTop.toString()))){//是置顶贴,默认不回复 所以在这里过滤掉
continue;
}
if(replyNum != null){
if(reply.intValue() == replyNum.intValue()){
list.add(element.attr("href").substring(3));
}
}else{
list.add(element.attr("href").substring(3));
}
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return list;
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID).get();
Elements element = doc.getElementsByAttributeValue("itemprop", "softwareVersion");
// .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
// .referrer("http://www.google.com")
if (element != null && !element.text().isEmpty())
newVersion = element.text();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "doInBackground: " + newVersion);
return newVersion;
}
public static String parseViewStateCode(String html) {
String code = "";
Document doc = org.jsoup.Jsoup.parse(html);
Elements inputs = doc.getElementsByAttributeValue("name", Url.__VIEWSTATE);
if (inputs.size() > 0) {
code = inputs.get(0).attr("value");
} else {
}
return code;
}
public static void main(String[] args) throws IOException {
String githubTrendingUrl = "https://github.com/trending/java/?since=weekly";
Document doc = Jsoup.connect(githubTrendingUrl).get();
Elements e = doc.getElementsByAttributeValue("class", "explore-content");
if (e.size() != 1) {
System.err.println("Error when parsing the page (explore-content)");
System.exit(-1);
}
Elements ol = e.get(0).getElementsByTag("ol");
if (ol.size() != 1) {
System.err.println("Error when parsing the page (ol)");
System.exit(-1);
}
Elements h3 = ol.get(0).getElementsByTag("h3");
List<String> results = new ArrayList<String>();
System.out.println("Nb trendings: "+h3.size());
for (Element project : h3) {
String link = project.getElementsByTag("a").get(0).attr("href");
String slugName = link.substring(1);
Repository repo = RepositoryHelper.getRepositoryFromSlug(slugName);
if (repo != null) {
Build b = repo.getLastBuild(false);
if (b != null && b.getBuildTool() == BuildTool.MAVEN) {
results.add(slugName);
}
}
}
System.out.println(StringUtils.join(results,"\n"));
}
/**
* Give a list of results appearing in the first result page for the key word in
* packetstormsecurity.org
*
* @param appName
* @param version
* @return
* @throws Exception
*/
public static ArrayList<String> fromPacketStorm(String appName, String version)
throws Exception {
// TODO extend this to fetch all result pages, currently it print only the 1st page
ArrayList<String> res = new ArrayList<String>();
URL url = new URL("http://packetstormsecurity.org/search/files/?q=" + appName + "&s=files");
WebPage wp = new WebPage(url);
Document document = wp.getDocument();
// System.out.println(document);
if ((document.text().contains("Your Request Returned Nothing of Interest"))) {
System.out.println("No Results Found in packetstormsecurity.org");
} else {
Elements elts = document.getElementsByAttributeValue("class", "ico text-plain");
for (Element elt : elts) {
Pattern p =
Pattern.compile(
"<a class=\"ico text-plain\" href=\"(.+)\" title=\"Size: (.+?) KB\">(.*)</a>");
Matcher m = p.matcher(elt.toString());
if (m.find()) {
String href = elt.attr("href");
res.add(href);
}
}
System.out.println("dad");
}
return res;
}
private Elements getElementsByAttributeAndValue(VRaptorTestResult navigationResult, String attributeName, String attributeValue) {
Document document = Jsoup.parse(navigationResult.getResponseBody());
return document.getElementsByAttributeValue(attributeName, attributeValue);
}