编码的UI-C#-ExecuteScript-计数未正确返回值 [英] Coded UI - C# - ExecuteScript - count does not return value correctly

查看:61
本文介绍了编码的UI-C#-ExecuteScript-计数未正确返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编码UI中使用ExecuteScript观察到奇怪的行为。当两个数字彼此相邻时,count不能返回正确的值。我不确定为什么会这样。这是代码段:

 静态已久YearQA = 2030; 
static long pastYearQA = 2029;

BrowserWindow窗口= new BrowserWindow();
window.WaitForControlEnabled();

long countCurrentYearQA =(long)window.ExecuteScript( count = 0; if(document.body.innerHTML.toString()。indexOf(' + nowYearQA.ToString()。Trim()+ ')> -1){count = 1;}返回计数;);
long countPastYearQA =(long)window.ExecuteScript( count = 0; if(document.body.innerHTML.toString()。indexOf(' + pastYearQA.ToString()。Trim()+')> ; -1){count = 1;}返回计数;);

MessageBox.Show(countCurrentYearQA + + countPastYearQA);

这是用于页面验证,其中我正在检查a的内部文本中是否存在某些数字页。如果存在数字,则返回1,如果不存在,则返回0。下面的讨论采用了ExecutionScript代码: https://forums.asp.net/t/1945825.aspx?javascript+check+if+a+string+exists + on + page



对于我测试过的内部网站,如果数字之间的差值至少为2,则count返回正确的值,但如果差异大于2。



为了进行测试,我使用了 https: //www.google.com -对于此网站,数字之间的差异必须为3,以获取正确的值。例如,如果nowYearQA = 2030且pastYearQA = 2029或pastYearQA = 2028,则countCurrentYearQA为0,countPastYearQA为1-这是错误的结果。并且pastYearQA = 2027,然后countCurrentYearQA变为0,countPastYearQA变为0-这是正确的结果。



我在这里俯瞰着什么吗?为什么会有这种差异?我从帖子指出 ExecuteScript API不支持Int,仅支持long-这就是我长期在代码片段中强制转换变量的原因。

解决方案

当前方法的问题是,您在整个标记源中搜索四位数的数字字符串,这很可能会出现在某处。 p>

我建议进行三项改进:


  1. 在innerText中搜索,而不是在innerHTML中搜索。这样,排除了不可见标签(如脚本)中的数字

  2. 专门针对包含年份数字的标签。检查标记以找到选择器的适当条件,例如id值:



    document.getElementByID('yearspan')。innerText


  3. 仅搜索整个单词,而不使用 indexOf 函数。在此处找到有关如何执行此操作的示例: https://stackoverflow.com/a/2232947/1132334



I am observing a strange behavior using ExecuteScript in Coded UI. When two numbers are next to each other, count does not return the correct value. I'm not sure why it's happening. Here is the code snippet:

static long nowYearQA = 2030;
static long pastYearQA = 2029;

BrowserWindow window = new BrowserWindow();
window.WaitForControlEnabled();

long countCurrentYearQA = (long)window.ExecuteScript("count = 0; if(document.body.innerHTML.toString().indexOf('" + nowYearQA.ToString().Trim() + "')  > -1){count = 1;} return count;");
long countPastYearQA = (long)window.ExecuteScript("count = 0; if(document.body.innerHTML.toString().indexOf('" + pastYearQA.ToString().Trim() + "')  > -1){count = 1;} return count;");

MessageBox.Show(countCurrentYearQA + " " + countPastYearQA);

This is for a page validation where I'm checking whether certain number presents in the Inner Text of a page. If number presents, then it returns 1 and if number does not exist, then it returns 0. ExecutionScript code was adopted from the following discussion: https://forums.asp.net/t/1945825.aspx?javascript+check+if+a+string+exists+on+page

For the internal website where I tested it, count returns correct value if the difference between numbers is at least 2, but returns incorrectly when difference is greater than 2.

For a test I used https://www.google.com - for this site, difference between the number needs to be 3 to get the correct value. For example, if nowYearQA = 2030 and pastYearQA = 2029, or pastYearQA = 2028, then countCurrentYearQA gets 0 and countPastYearQA gets 1 - this is incorrect result.

When nowYearQA = 2030 and pastYearQA = 2027, then countCurrentYearQA gets 0 and countPastYearQA gets 0 - this is correct result.

Is there something I am overlooking here? Why is this difference? I understand from this post that "ExecuteScript API does not support Int, and only supports long" - this is the reason I've long cast the variables in the code snippet.

解决方案

The problem with your current approach is that you search the entire markup source for four-digit numeric strings, which are likely to occur somewhere.

I suggest three improvements:

  1. Search in innerText, not in innerHTML. In this way numbers that are part of invisible tags like scripts, are excluded
  2. Target the tag(s) that contain the year numbers specifically. Inspect the markup to find the appropriate criteria for a selector, for example an id value:

    document.getElementByID('yearspan').innerText

  3. search for whole words only, not with the indexOf function. Find an example of how that can be done here: https://stackoverflow.com/a/2232947/1132334

这篇关于编码的UI-C#-ExecuteScript-计数未正确返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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