代码之家  ›  专栏  ›  技术社区  ›  tedtrain

TypeError:无法读取未定义的属性“goto”

  •  0
  • tedtrain  · 技术社区  · 3 年前

    当我试图运行我的测试时,我得到了这个错误。我不熟悉打字脚本和剧作家,但不熟悉编程/测试自动化。

    这是登录页面代码

    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”

    有人能告诉我我做错了什么吗?

    谢谢

    1 回复  |  直到 3 年前
        1
  •  1
  •   archon    3 年前

    这个 Page 是吗 interface 用于浏览页面。你已经定义了 readonly page: Page 在你的 LoginPage 但是你没有分配给任何东西。因此,对于 登录页面 .在构造函数中,您应该调用 this.page = page;