必须使用量角器在名称和电子邮件的占位符中发送值
Have to send values in placeholder for Name and Email using protractor
以下是 HTML,其中我们有姓名和电子邮件的占位符
<section slot="drawer" for="account" class="iron-selected">
<form class="form ng-pristine ng-valid">
<h4>CONTACT US</h4>
<p type="Name:"><input placeholder="Write your name here.."></p>
<p type="Email:"><input placeholder="Let us know how to contact you back..">
</p>
<button>Send Message</button>
</form>
</section>
取决于你到底想改变什么。
更改属性值
可以使用 "pure" javascript 更改属性值。这可以通过 browser.executeScript('your javascript code here');
完成,可以找到文档 here
考虑到更改 属性 -值:
- 在我看来是单元测试,而不是 UI 测试
- 用户永远无法通过 UI 更改(仅当他使用开发人员工具时)
- 占位符提示用户可以在控件中输入什么。占位符文本不得包含回车符 returns 或换行符。
注意:不要使用占位符属性代替元素,它们的用途不同。该属性描述了表单元素的作用(即它表示期望的信息类型),而占位符属性是关于内容应采用的格式的提示。在某些情况下,占位符属性永远不会显示给用户,因此没有它,表单必须是可以理解的。(source 并搜索占位符)
更改输入
如果您想发送一个值(因此不更改 placeholder
-属性),您可以使用 sendKeys()
-方法。有关如何使用它的更多信息,请查看 here
在您的情况下,您首先需要确定要将值发送到的元素。如果您需要向其发送值的 input
元素具有唯一的选择器,那就太好了。但是用你给的代码你可以用这个解决它
// Because you can't select the input directly we can access it by it's parent
// `p` tag and then go to the input
element(by.css('p[type="Name:"] input')).sendKeys('my name is');
element(by.css('p[type="Email:"] input')).sendKeys('e.mail@example.com');
最后,您需要点击 button
提交您的表格。因为您的 button
没有唯一选择器,您可以使用 by.buttonText
方法。这不是推荐的方法,因为您不想根据文本查找元素。但现在你可以做到这一点
element(by.buttonText('Send Message'));
您的完整脚本将变成
element(by.css('p[type="Name:"] input')).sendKeys('my name is');
element(by.css('p[type="Email:"] input')).sendKeys('e.mail@example.com');
element(by.buttonText('Send Message'));
希望这对您有所帮助。
以下是 HTML,其中我们有姓名和电子邮件的占位符
<section slot="drawer" for="account" class="iron-selected">
<form class="form ng-pristine ng-valid">
<h4>CONTACT US</h4>
<p type="Name:"><input placeholder="Write your name here.."></p>
<p type="Email:"><input placeholder="Let us know how to contact you back..">
</p>
<button>Send Message</button>
</form>
</section>
取决于你到底想改变什么。
更改属性值
可以使用 "pure" javascript 更改属性值。这可以通过 browser.executeScript('your javascript code here');
完成,可以找到文档 here
考虑到更改 属性 -值:
- 在我看来是单元测试,而不是 UI 测试
- 用户永远无法通过 UI 更改(仅当他使用开发人员工具时)
- 占位符提示用户可以在控件中输入什么。占位符文本不得包含回车符 returns 或换行符。 注意:不要使用占位符属性代替元素,它们的用途不同。该属性描述了表单元素的作用(即它表示期望的信息类型),而占位符属性是关于内容应采用的格式的提示。在某些情况下,占位符属性永远不会显示给用户,因此没有它,表单必须是可以理解的。(source 并搜索占位符)
更改输入
如果您想发送一个值(因此不更改 placeholder
-属性),您可以使用 sendKeys()
-方法。有关如何使用它的更多信息,请查看 here
在您的情况下,您首先需要确定要将值发送到的元素。如果您需要向其发送值的 input
元素具有唯一的选择器,那就太好了。但是用你给的代码你可以用这个解决它
// Because you can't select the input directly we can access it by it's parent
// `p` tag and then go to the input
element(by.css('p[type="Name:"] input')).sendKeys('my name is');
element(by.css('p[type="Email:"] input')).sendKeys('e.mail@example.com');
最后,您需要点击 button
提交您的表格。因为您的 button
没有唯一选择器,您可以使用 by.buttonText
方法。这不是推荐的方法,因为您不想根据文本查找元素。但现在你可以做到这一点
element(by.buttonText('Send Message'));
您的完整脚本将变成
element(by.css('p[type="Name:"] input')).sendKeys('my name is');
element(by.css('p[type="Email:"] input')).sendKeys('e.mail@example.com');
element(by.buttonText('Send Message'));
希望这对您有所帮助。