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

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 19, 2025

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

      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

      Computex

      May 19, 2025

      DOOM: The Dark Ages gets Path Tracing update in June, bringing better visuals for PC players

      May 19, 2025

      Early Memorial Day deals are LIVE on Windows PCs, gaming accessories, and more — 6 hand-picked discounts on our favorites

      May 19, 2025

      Microsoft open sources the Windows Subsystem for Linux — invites developers to help more seamlessly integrate Linux with Windows

      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

      How JavaScript’s at() method makes array indexing easier

      May 19, 2025
      Recent

      How JavaScript’s at() method makes array indexing easier

      May 19, 2025

      Motherhood and Career Balance in Tech: Stories from Perficient LATAM

      May 19, 2025

      ES6: Set Vs Array- What and When?

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

      Computex

      May 19, 2025
      Recent

      Computex

      May 19, 2025

      DOOM: The Dark Ages gets Path Tracing update in June, bringing better visuals for PC players

      May 19, 2025

      Early Memorial Day deals are LIVE on Windows PCs, gaming accessories, and more — 6 hand-picked discounts on our favorites

      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

    Development

    February 2025 Baseline monthly digest

    May 19, 2025
    Development

    How Cybercriminals Crack Your Passwords (And How to Stay One Step Ahead)

    May 19, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Le notizie minori del mondo GNU/Linux e dintorni della settimana nr 15/2025

    Linux

    Samsung Galaxy Z Flip 6 hands-on: The foldable most people should buy gets one key upgrade

    Development

    str0m is a Sans I/O WebRTC implementation

    Linux

    The Loper Bright Decision: How it Impacts Cybersecurity Law

    Development
    Hostinger

    Highlights

    News & Updates

    “Fear not—we are cooking!” Helldivers 2 devs say there’s “exciting news to come” and a new Warbond in May as we defeat the Illuminate, which surely means it’s about to invade for real and kill us all

    April 29, 2025

    Helldivers 2 developer Arrowhead says there’s “exciting news to come” and a new Warbond coming…

    Windows 11 Shutdown But User Stays Logged in: How to Fix it

    February 25, 2025

    [Webinar] Oracle Project-Driven Supply Chain at Roeslein & Associates

    December 20, 2024

    200+ artists warn about generative AI meddling in music

    April 4, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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