TypeError: Cannot read property 'goto' of undefined
TypeError: Cannot read property 'goto' of undefined
当我尝试 运行 我的测试时,出现了这个错误。我是打字稿和剧作家的新手,但不是 programming/Test 自动化。
这是登录页面代码
import { Locator, Page } from "@playwright/test";
export class LoginPage {
readonly page: Page;
readonly input_userId: Locator;
readonly input_password: Locator;
readonly button_login : Locator;
constructor(page:Page){
// super(page)
this.input_userId = page.locator('id=loginForm:userNameInput');
this.input_password = page.locator('id=loginForm:passwordInput');
this.button_login = page.locator('id=loginForm:loginButton');
}
async goto(url:string) {
await this.page.goto(url);
}
async enterUserName(userName:string){
await this.input_userId.fill(userName)
}
async enterPassword(password:string){
await this.input_password.fill(password)
}
async clickLogin(){
await this.button_login.click();
}
}
这就是我运行我的测试我的测试
import {test,expect,Page} from '@playwright/test'
import { LoginPage } from '../pages/loginPage'
test.describe('Login Page', async () => {
test('Login as a super user',async ({page}) => {
const loginpage = new LoginPage(page);
loginpage.goto('https:// Some_URL'); // This is failing here
loginpage.enterUserName('test@test.com');
loginpage.enterPassword('test')
loginpage.clickLogin();
})
});
TypeError: 无法读取未定义的 属性 'goto'
谁能告诉我我做错了什么?
谢谢
Page
是浏览器页面的 interface
。您已经在 LoginPage
class 中定义了 readonly page: Page
但是您没有将其分配给任何东西。因此它对于 LoginPage
是未定义的。在你的构造函数中,你应该调用 this.page = page;
当我尝试 运行 我的测试时,出现了这个错误。我是打字稿和剧作家的新手,但不是 programming/Test 自动化。
这是登录页面代码
import { Locator, Page } from "@playwright/test";
export class LoginPage {
readonly page: Page;
readonly input_userId: Locator;
readonly input_password: Locator;
readonly button_login : Locator;
constructor(page:Page){
// super(page)
this.input_userId = page.locator('id=loginForm:userNameInput');
this.input_password = page.locator('id=loginForm:passwordInput');
this.button_login = page.locator('id=loginForm:loginButton');
}
async goto(url:string) {
await this.page.goto(url);
}
async enterUserName(userName:string){
await this.input_userId.fill(userName)
}
async enterPassword(password:string){
await this.input_password.fill(password)
}
async clickLogin(){
await this.button_login.click();
}
}
这就是我运行我的测试我的测试
import {test,expect,Page} from '@playwright/test'
import { LoginPage } from '../pages/loginPage'
test.describe('Login Page', async () => {
test('Login as a super user',async ({page}) => {
const loginpage = new LoginPage(page);
loginpage.goto('https:// Some_URL'); // This is failing here
loginpage.enterUserName('test@test.com');
loginpage.enterPassword('test')
loginpage.clickLogin();
})
});
TypeError: 无法读取未定义的 属性 'goto'
谁能告诉我我做错了什么?
谢谢
Page
是浏览器页面的 interface
。您已经在 LoginPage
class 中定义了 readonly page: Page
但是您没有将其分配给任何东西。因此它对于 LoginPage
是未定义的。在你的构造函数中,你应该调用 this.page = page;