Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
1
Indexable
Never
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;
using static System.Net.Mime.MediaTypeNames;

namespace PlaywrightDemo
{
    public class NUnitPlaywright : PageTest
    {
        private string _username = "admin";
        private string _password = "password";
        private string _username2 = "testscuser1";
        private string _password2 = "Unikalnehaslo1";

        [SetUp]
        public async Task Setup()
        {
            await Page.GotoAsync(url: "https://test.supportcentral.pwc.com/help/?id=sc_login");
        }

        [Test]
        public async Task Test1()
        {
            var loginPage = new LoginPage(Page);
            await loginPage.ClickAdditionalOptions();
            await loginPage.EnterInvalidCredentials(_username, _password);
            await loginPage.ClickLogin();
            await loginPage.ExpectInvalidCredentialsError();
        }

        [Test]
        public async Task Test2()
        {
            var loginPage = new LoginPage(Page);
            await loginPage.ClickAdditionalOptions();
            await loginPage.EnterValidCredentials(_username2, _password2);
            await loginPage.ClickLogin();
            await loginPage.ExpectValidCredentialsAfterLogin();
        }

        public class LoginPage
        {
            private readonly IPage_page;
    
    public LoginPage(IPage page)
            {
                _page = page;
            }

            public async Task ClickAdditionalOptions()
            {
                var lnkLogin = Page.Locator(selector: "text=Additional options");
                await lnkLogin.ClickAsync();
            }

            public async Task EnterInvalidCredentials(string username, string password)
            {
                await Page.FillAsync(selector: "#username", value: username);
                await Page.FillAsync(selector: "#password", value: password);
            }

            public async Task ClickLogin()
            {
                var btnLogin = Page.Locator(selector: "//*[@id=\"xcde58fd91b533410516b0ed2cd4bcb3a\"]/div/form/div/div[2]/div[2]/div/div[2]/button", new PageLocatorOptions { HasTextString = "Log in" });
                await btnLogin.ClickAsync();
            }

            public async Task ExpectInvalidCredentialsError()
            {
                await Expect(Page.Locator(selector: "text='User name or password invalid'")).ToBeVisibleAsync();
            }

            public async Task EnterValidCredentials(string username, string password)
            {
                await Page.FillAsync(selector: "#username", value: username);
                await Page.FillAsync(selector: "#password", value: password);
            }

            public async Task ExpectValidCredentialsAfterLogin()
            {
                await Expect(Page.Locator(selector: "text='Login successful'")).ToBeVisibleAsync();
            }
        }
    }
}