Coded UI - C# - time itemprop tag - how to validate year exists

回眸只為那壹抹淺笑 提交于 2019-12-13 04:23:01

问题


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();"));

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.

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>

回答1:


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);
});


来源:https://stackoverflow.com/questions/50765069/coded-ui-c-sharp-time-itemprop-tag-how-to-validate-year-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!