Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 19, 2025

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 19, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 19, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 19, 2025

      My latest hands-on could be the best value AI laptop of the summer, but I still have questions

      May 19, 2025

      DOOM: The Dark Ages had the lowest Steam launch numbers in series history — Is it suffering from the ‘Game Pass Effect’?

      May 19, 2025

      Microsoft won’t be left exposed if something “catastrophic” happens to OpenAI — but may still be 3 to 6 months behind ChatGPT

      May 19, 2025

      Microsoft Copilot gets OpenAI’s GPT-4o image generation support — but maybe a day late and a dollar short for the hype?

      May 19, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      ES6: Set Vs Array- What and When?

      May 19, 2025
      Recent

      ES6: Set Vs Array- What and When?

      May 19, 2025

      Transform JSON into Typed Collections with Laravel’s AsCollection::of()

      May 19, 2025

      Deployer

      May 19, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      My latest hands-on could be the best value AI laptop of the summer, but I still have questions

      May 19, 2025
      Recent

      My latest hands-on could be the best value AI laptop of the summer, but I still have questions

      May 19, 2025

      DOOM: The Dark Ages had the lowest Steam launch numbers in series history — Is it suffering from the ‘Game Pass Effect’?

      May 19, 2025

      Microsoft won’t be left exposed if something “catastrophic” happens to OpenAI — but may still be 3 to 6 months behind ChatGPT

      May 19, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Playwright – Target Closed before any action

    Playwright – Target Closed before any action

    August 6, 2024

    I am trying playwright and try to automate login feature. I have page classes, test classes and separate action class to write common actions like click,enter inputs.

    My common action class

    import { expect, Locator, Page } from “@playwright/test”

    export default class Actions {

    page:Page;

    constructor(page:Page) {
    this.page = page ;
    }

    public async enterInput(value:string,element:Locator) {
    element.fill(value);
    await this.page.waitForTimeout(5000);
    }

    public async clickButton(element:Locator) {
    element.click();
    await this.page.waitForTimeout(5000)
    }

    }

    My login.page.ts class

    import { Locator, Page, expect } from “@playwright/test”;
    import Actions from “../common/actions”;
    import * as path from “../xpaths/xpath.json”
    import fs from ‘fs’;

    export default class LoginPage {

    page: Page;
    action:Actions;

    inputUsername:Locator;
    inputPassword:Locator;
    btnLogin:Locator;
    errorText:Locator;

    constructor(page:Page) {
    this.page = page ;
    this.action=new Actions(page);

    const pathsxData = fs.readFileSync(“json path here”, “utf-8”);
    const pathsx = JSON.parse(pathsxData);

    this.inputUsername = this.page.locator(path.usernameInput);
    this.inputPassword = this.page.locator(path.passwordInput);
    this.btnLogin = this.page.locator(path.loginBtn);
    this.errorText=this.page.locator(path.loginError)
    }

    public async login(username:string,password:string)
    {
    this.action.enterInput(username,this.inputUsername);
    this.action.enterInput(password,this.inputPassword);
    await this.action.clickButton(this.btnLogin);
    }

    public async successLogin(username:string,password:string,title:string){
    this.login(username,password);
    await expect(this.page).toHaveTitle(title);
    }

    public async invalidLogin(username:string,password:string,error:string){
    this.login(username,password);
    await expect(this.errorText).toHaveText(error);
    }

    public async emptyLogin(username:string,password:string,error:string){
    this.login(username,password);
    await expect(this.errorText).toHaveText(error);
    }

    }

    My logintest.spec.ts

    import { test , expect} from “@playwright/test”
    import LoginPage from “../pages/login.page”
    import * as data from “../testdata/testdata.json”

    test.describe(‘Login Test’, () =>{
    let login:LoginPage;

    test.beforeEach(async({page}) =>{
    await page.goto(“My Url”);
    await page.waitForTimeout(5000)
    login = new LoginPage(page);
    });

    test(‘invalid login-username/password both incorrect’, async () => {
    login.invalidLogin(data.invalid_username,data.invalid_password,data.invalid_error);
    });

    test(’empty username/password login’, async()=>{
    login.invalidLogin(data.invalid_username,data.invalid_password,data.invalid_error);
    });
    test(‘successfully login to the system’ , async () => {
    login.successLogin(data.username,data.password,data.dashboard_title);
    });
    });

    Whenever I execute the code always getting Target Closed error.Below is the error

    Error: locator.fill: Target closed
    =========================== logs ===========================
    waiting for locator(‘xpath=//input[contains(@id,’outlined-basic’) and contains(@type,’text’)]’)
    ============================================================

    at ..commonactions.ts:16

    14 |
    15 | public async enterInput(value:string,element:Locator) {
    > 16 | element.fill(value);
    | ^
    17 | await this.page.waitForTimeout(5000);
    18 | }

    But when I execute code without using action class like below code executes successfully.

    import { test , expect} from “@playwright/test”
    import LoginPage from “../pages/login.page”
    import * as data from “../testdata/testdata.json”

    test.describe(‘Login Test’, () =>{
    let login:LoginPage;

    test.beforeEach(async({page}) =>{

    console.log(page.url)

    login = new LoginPage(page);
    });

    test(‘invalid login-username/password both incorrect’, async ({page}) => {
    page.goto(“http://192.168.0.127/ussd_qa/login”);
    await page.waitForTimeout(5000);
    await login.inputUsername.fill(data.username);
    await page.waitForTimeout(5000);
    await login.inputPassword.fill(data.password);
    await page.waitForTimeout(5000);
    await login.btnLogin.click();
    await page.waitForTimeout(5000);
    await expect(login.errorText).toHaveText(data.invalid_error);
    });
    });

    Can someone explain what is wrong here?

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow Software Testing Failures Led to a Global Crisis: Key Takeaways
    Next Article Drag & Drop issue in selenium web driver

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 19, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4915 – PHPGurukul Auto Taxi Stand Management System SQL Injection

    May 19, 2025
    Leave A Reply Cancel Reply

    Hostinger

    Continue Reading

    How to use the Orca screen reader in Linux

    Development

    Web Design Services Market Is Going to Boom

    Development

    Quantum Motion partners with GlobalFoundries to create more powerful chips

    News & Updates

    What’s new with GitHub Copilot: July 2024

    Development

    Highlights

    Development

    Phishing Attack at Los Angeles County Department of Public Health Leads to Major Data Breach

    June 18, 2024

    The Los Angeles County Department of Public Health (DPH) has disclosed a significant data breach…

    Selenium Java best practices for handling different languages and locations, and setting dynamic architecture

    July 9, 2024

    Key Trends and Forecasts Influencing the GenAI Market in 2024

    June 21, 2024

    Distributor of ANOM Encrypted Devices Sentenced to Over 5 Years in Prison

    November 18, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.