类型错误:无法读取属性“goto”未定义的
当我尝试运行测试时,出现此错误。我是打字稿和剧作家的新手,但不是编程/测试自动化。
这是登录页面代码
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('[email protected]');
loginpage.enterPassword('test')
loginpage.clickLogin();
})
});
类型错误:无法读取未定义的属性“goto”
有人可以让我知道我做错了什么吗?
谢谢
When I try to run my test, I am getting this error. I am new to typescript and playwright, but not programming/Test Automation.
This is the login page code
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();
}
}
This is where I am running my test my test
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('[email protected]');
loginpage.enterPassword('test')
loginpage.clickLogin();
})
});
TypeError: Cannot read property 'goto' of undefined
Can someone let me know what I am doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Page
是浏览器页面的界面
。您已在LoginPage
类中定义了readonly page: Page
,但未将其分配给任何内容。因此LoginPage
未定义。在构造函数中,您应该调用this.page = page;
The
Page
is theinterface
for a browser page. You have definedreadonly page: Page
in yourLoginPage
class however you didn't assign it to anything. Therefore it's undefined for theLoginPage
. Inside your constructor, you should callthis.page = page;