如何使用 Protractor with Jasmine 验证某些下拉菜单中的默认值是否为当前日期
How to verify if the default values in some drop downs are the current date using Protractor with Jasmine
我是量角器的新手,我正在验证某些下拉菜单中的默认值是否为当前日期:月、日和年
这是我写的代码:
this.startDateMonthDropdown = element(by.xpath("//op-dropdown[@change.bind='changeStartMonth']"));
this.startDayInput = element(by.xpath("//input[@value.two-way='startDay']"));
this.startYearDropdown = element(by.xpath("//op-dropdown[@change.bind='changeStartYear']"));
function checkStartDateIsCurrentDate() {
let date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
let currentMonth = date.getMonth() + 1;
let currentDay = date.getDate() - 1;
let currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toEqual(currentMonth)
});
this.startDayInput.getAttribute('value').then(function (day) {
expect(day).toEqual(currentDay)
});
this.startYearDropdown.getText().then(function (year) {
expect(year).toEqual(currentYear)
});
}
当我运行测试时,它失败了:
- 预期 'July' 等于 7。
- 预期“4”等于 4。
- 预期“2019”等于 2019。
- Expected '4' to equal 4.
- Expected '2019' to equal 2019.
当您与元素交互并编写 getText() 时。它将值作为文本,但如果您想与数字进行比较,则必须将文本转换为数字,然后进行验证。
getText()
方法 returns 一个将用元素的可见文本(字符串)解析的承诺。所以你必须将 currentDay
和 currentYear
转换为字符串。
function checkStartDateIsCurrentDate() {
const date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
const currentMonth = date.toLocaleString('en-us', { month: 'long' });
const currentDay = date.getDate() - 1;
const currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toBe(currentMonth)
});
this.startDayInput.getAttribute('value').then(function (day) {
expect(day).toBe(`${currentDay}`)
});
this.startYearDropdown.getText().then(function (year) {
expect(year).toBe(`${currentYear}`)
});
}
感谢您的回答。我设法修复它并解决了这个月的问题。
这是我的最终代码
函数 checkStartDateIsCurrentDate() {
let date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
let currentMonth = date.getMonth();
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let currentDay = date.getDate() - 1;
let currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toEqual(months[currentMonth])
});
this.startDayInput.getAttribute('value').then(function (day) {
day = parseInt(day);
expect(day).toEqual(currentDay)
});
this.startYearDropdown.getText().then(function (year) {
year = parseInt(year);
expect(year).toEqual(currentYear)
});
}
我是量角器的新手,我正在验证某些下拉菜单中的默认值是否为当前日期:月、日和年
这是我写的代码:
this.startDateMonthDropdown = element(by.xpath("//op-dropdown[@change.bind='changeStartMonth']"));
this.startDayInput = element(by.xpath("//input[@value.two-way='startDay']"));
this.startYearDropdown = element(by.xpath("//op-dropdown[@change.bind='changeStartYear']"));
function checkStartDateIsCurrentDate() {
let date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
let currentMonth = date.getMonth() + 1;
let currentDay = date.getDate() - 1;
let currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toEqual(currentMonth)
});
this.startDayInput.getAttribute('value').then(function (day) {
expect(day).toEqual(currentDay)
});
this.startYearDropdown.getText().then(function (year) {
expect(year).toEqual(currentYear)
});
}
当我运行测试时,它失败了:
- 预期 'July' 等于 7。
- 预期“4”等于 4。
- 预期“2019”等于 2019。
- Expected '4' to equal 4.
- Expected '2019' to equal 2019.
当您与元素交互并编写 getText() 时。它将值作为文本,但如果您想与数字进行比较,则必须将文本转换为数字,然后进行验证。
getText()
方法 returns 一个将用元素的可见文本(字符串)解析的承诺。所以你必须将 currentDay
和 currentYear
转换为字符串。
function checkStartDateIsCurrentDate() {
const date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
const currentMonth = date.toLocaleString('en-us', { month: 'long' });
const currentDay = date.getDate() - 1;
const currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toBe(currentMonth)
});
this.startDayInput.getAttribute('value').then(function (day) {
expect(day).toBe(`${currentDay}`)
});
this.startYearDropdown.getText().then(function (year) {
expect(year).toBe(`${currentYear}`)
});
}
感谢您的回答。我设法修复它并解决了这个月的问题。 这是我的最终代码
函数 checkStartDateIsCurrentDate() {
let date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
let currentMonth = date.getMonth();
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let currentDay = date.getDate() - 1;
let currentYear = date.getFullYear();
this.startDateMonthDropdown.getText().then(function (month) {
expect(month).toEqual(months[currentMonth])
});
this.startDayInput.getAttribute('value').then(function (day) {
day = parseInt(day);
expect(day).toEqual(currentDay)
});
this.startYearDropdown.getText().then(function (year) {
year = parseInt(year);
expect(year).toEqual(currentYear)
});
}