Nội dung bài học

Selenium C# Bài 2 sẽ giới thiệu đến các bạn các cách lấy Element và thực hành thao tác với chúng trong Selenium C#.

Ví dụ 1: Nhấp vào liên kết bằng XPATH Locator:

Kịch bản thử nghiệm:

1. Điều hướng đến trang web Demo - https://demoqa.com/
2. Phóng to cửa sổ Chrome
3. Nhấp vào menu "Element"

Demo Page
 4. Nhấp vào menu "Text Box"
 Text box

Code mẫu:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using VTACourses.PageObjects;

namespace VTACourses
{
    class SeleniumDemo
    {
        IWebDriver driver;


        [SetUp]
        public void startBrowser()
        {
            driver = new ChromeDriver("D:\\TESTER\\Automation Testing\\Project"); //Thư mục cuối cùng chứa File Chromedriver.exe
            driver.Manage().Window.Maximize(); //Để bung fullscreen màn hình cái Chrome
        }


        [Test]
        public void DemoGetElement()
        {
            driver.Url = "https://demoqa.com/";
            Thread.Sleep(3000);

            IWebElement moduleElement = driver.FindElement(By.XPath("//*[name()='path' and contains(@d,'M16 132h41')]"));
            moduleElement.Click();
            Thread.Sleep(2000);
            IWebElement menuTextBox = driver.FindElement(By.XPath("//span[contains(text(),'Text Box')]"));
            menuTextBox.Click();
            Thread.Sleep(2000);
        }​


        [TearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}

Ví dụ 2: Nhập dữ liệu vào TextBox và nhấp vào nút Submit bằng cách sử dụng bộ định vị ID:

Kịch bản thử nghiệm:

  1. Tại vị trí của Ví dụ 1 chúng ta tiếp tục
  2. Nhập dữ liệu vào các ô Input
  3. Bấm vào nút "Submit"
  4. Check thông tin bên dưới
- Name
- Email
- Current Address
- Permananet Address

Text Input

Code mẫu:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using VTACourses.PageObjects;

namespace VTACourses
{
    class SeleniumDemo
    {
        IWebDriver driver;


        [SetUp]
        public void startBrowser()
        {
            driver = new ChromeDriver("D:\\TESTER\\Automation Testing\\Project"); //Thư mục cuối cùng chứa File Chromedriver.exe
            driver.Manage().Window.Maximize(); //Để bung fullscreen màn hình cái Chrome
        }


        [Test]
        public void DemoGetElement()
        {
            driver.Url = "https://demoqa.com/";
            Thread.Sleep(5000);
            IWebElement moduleElement = driver.FindElement(By.XPath("//*[name()='path' and contains(@d,'M16 132h41')]"));
            moduleElement.Click();
            Thread.Sleep(2000);
            IWebElement menuTextBox = driver.FindElement(By.XPath("//span[contains(text(),'Text Box')]"));
            menuTextBox.Click();
            Thread.Sleep(2000);

            //SendKeys (Nhập liệu)
            IWebElement txtFullName = driver.FindElement(By.Id("userName"));
            IWebElement txtEmail = driver.FindElement(By.Id("userEmail"));
            IWebElement txtCurentAddress = driver.FindElement(By.Id("currentAddress"));
            IWebElement txtPermanentAddress = driver.FindElement(By.Id("permanentAddress"));
            //Click (Button)
            IWebElement btnSubmit = driver.FindElement(By.Id("submit"));

            txtFullName.SendKeys("VTA Courses");
            Thread.Sleep(1000);
            txtEmail.SendKeys("abc@email.com");
            Thread.Sleep(1000);
            txtCurentAddress.SendKeys("Đồng Tháp");
            Thread.Sleep(1000);
            txtPermanentAddress.SendKeys("Cần Thơ");
            Thread.Sleep(1000);
            btnSubmit.Click();
            Thread.Sleep(3000);
        }


        [TearDown]
        public void closeBrowser()
        {
            driver.Close();
        }
    }
}


Cộng đồng Automation Testing Việt Nam

🌱 Facebook Fanpage: Anh Tester
🌱 Telegram
Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

  • Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu


Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ kiến thức lên trang

Bạn có thể đăng bài để chia sẻ kiến thức, bài viết của chính bạn lên trang Anh Tester Blog

Danh sách bài học