编码的用户界面-C#-时间itemprop标签-如何验证年份是否存在 [英] Coded UI - C# - time itemprop tag - how to validate year exists

查看:81
本文介绍了编码的用户界面-C#-时间itemprop标签-如何验证年份是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 @dlatikay 中获得了帮助,该帮助与

I received help from @dlatikay in a slightly different question yesterday, that helped me resolving false positive error partially. Here is the code snippet:

string countCurrentYearQA = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + nowYearQAString + "')  > -1){count = 1;} return count.toString();"));
string countPastYearQA = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + pastYearQAString + "')  > -1){count = 1;} return count.toString();"));

string countCurrentYearQAWithoutEndPipe = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + nowYearQAStringWithoutPipe + "')  > -1){count = 1;} return count.toString();"));
string countPastYearQAWithoutEndPipe = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + pastYearQAStringWithoutPipe + "')  > -1){count = 1;} return count.toString();"));

正在发生的事情是,在大多数情况下,JavaScript能够检查是否存在任何特定的年份.但是在页面上的任何地方都可能提到了一年,这可能会导致假阳性测试.

What is happening is that in most cases JavaScript is able to check whether any particular year exists or not. But there maybe a year mentioned in anywhere on the page, that may cause false positive test.

我正在寻找一个示例,其中我可以执行JavaScript来定位时间标记,因为它具有带有itemprop ="datePublished"的datetime值.我只需要检查年份是否匹配.获取所有itemprop值的方法是什么,仅提取年份(4位数字),并且如果year与我在执行脚本中使用的年字符串匹配(例如:nowYearQAString),则count返回1,否则count返回0是否可以将所有年份都放在可以使用C#代码引用的列表/数组中?我将感谢任何将我引向正确方向的示例代码.

I'm seeking an example where I can execute JavaScript to target the time tag instead as it has the datetime value with itemprop="datePublished". I only need to check whether year matches. What is the way to get all itemprop values, extract only the year (4 digits), and if year matches with the year string I'm using in Execution Script (for example: nowYearQAString), then count returns 1, otherwise count returns 0. Is it possible to put all years in a list/array, that I can refer to using C# code? I will be thankful for any sample code that will direct me to the right direction.

这是我正在测试的页面的摘要.

Here is the snippet of the page I'm testing.

<time itemprop="datePublished" datetime="2018-03-08T11:00:00">March 8, 2018</time>

<time itemprop="datePublished" datetime="2017-12-07T12:40:00">December 7, 2017</time>

<time itemprop="datePublished" datetime="2017-11-23T10:00:00">November 23, 
2017</time>

<time itemprop="datePublished" datetime="2017-10-13T09:00:00">October 13, 2017</time>

<time itemprop="datePublished" datetime="2017-09-16T00:00:00">September 16, 2017</time>

<time itemprop="datePublished" datetime="2017-08-25T00:00:00">August 25, 2017</time>

<time itemprop="datePublished" datetime="2017-08-17T11:00:00">August 17, 2017</time>

<time itemprop="datePublished" datetime="2017-07-25T00:00:00">July 25, 2017</time>

<time itemprop="datePublished" datetime="2017-05-23T00:00:00">May 23, 2017</time>

<time itemprop="datePublished" datetime="2017-05-12T15:00:00">May 12, 2017</time>

推荐答案

这应该可以帮助您入门.它将把年份添加到数组中:

This should get you started. It will add the years to an array:

$(document).ready(function(){
  var years = [];
    $('time').each(function(){
     var thisYear = $(this).attr('datetime').split('-')[0];
     // do something with thisYear...
     years.push(thisYear);
  });
  // Write all years to console window.
  console.log(years);
});

这篇关于编码的用户界面-C#-时间itemprop标签-如何验证年份是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆