使用Selenium

时间:2015-05-26 21:39:49

标签: angularjs selenium selenium-webdriver webdriver

我有两个输入如下:

第一

<input type="text" class="form-control ng-isolate-scope ng-valid-date ng-animate ng-dirty ng-valid ng-valid-required" name="measurementDate" id=
"measurementDate" placeholder="MM/DD" datepicker-popup="MM/dd" min="01/01/2014" max="12/31/2016" datepicker-options="{&quot;showWeeks&quot;:false,&quot;yearRange&quot;:3}" ng-model="acawiz.data.standardPeriodStart" ng-change="acawiz.setStandardStabilityPeriodStart()" is-open="acawiz.measurementPickerIsOpen" style="">

第二

<input type="text" class="form-control ng-isolate-scope ng-pristine ng-valid-required ng-valid ng-valid-date" name="stabilityDate" id="stabilityDate" placeholder="MM/DD" show-weeks="false" datepicker-popup="MM/dd" min="01/01/2014" max="12/31/2016" datepicker-options="{&quot;showWeeks&quot;:false,&quot;yearRange&quot;:3}" ng-model="acawiz.data.standardStabilityPeriodStart" ng-change="acawiz.standardStabilityPeriodStartChanged()" is-open="acawiz.stabilityPickerIsOpen" ng-required="true" readonly="" required="required">

使用日期选择器(作为最终用户)填充这些日期。但是,对于Selenium测试,我决定绕过日期选择器,因为这可以使测试变得更加笨拙。要解决此问题,我最初考虑删除requiredreadonly属性,并使用sendKeys()输入日期,我就成功了。我还试图设置它们的值属性,我看到更改发生在以下:

string value = Convert.ToDateTime(date).ToString("MM/dd");
string setValueProp = @"document.getElementById('measurementDate').value = '" + value + "'";
ExecuteJavaScript(setValueProp);

但是,当我单击完成向导时,我没有看到我在日期中所做的更改,可能的原因是因为ng-change="acawiz.standardStabilityPeriodStartChanged()"从未触发过。有没有办法可以使用上面的javascript代码相应地触发ng-change?

1 个答案:

答案 0 :(得分:3)

当您以编程方式更改模型时(就像您正在做的那样),不会触发ng-change事件。我想你必须手动调用change事件。

string value = Convert.ToDateTime(date).ToString("MM/dd");
string setValueProp = @"$('#measurementDate').val('" + value + "').trigger('change');";
ExecuteJavaScript(setValueProp);