类型错误:无法读取属性“goto”未定义的

发布于 2025-01-11 09:31:54 字数 1575 浏览 0 评论 0原文

当我尝试运行测试时,出现此错误。我是打字稿和剧作家的新手,但不是编程/测试自动化。

这是登录页面代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2025-01-18 09:31:54

Page 是浏览器页面的界面。您已在 LoginPage 类中定义了 readonly page: Page,但未将其分配给任何内容。因此 LoginPage 未定义。在构造函数中,您应该调用 this.page = page;

The Page is the interface for a browser page. You have defined readonly page: Page in your LoginPage class however you didn't assign it to anything. Therefore it's undefined for the LoginPage. Inside your constructor, you should call this.page = page;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文