FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Selenium with C#: How to locate the table element and cells #1

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago

Introduction

This is the table to be tested:

Screen Shot 2020-09-25 at 9 32 48 AM

HTML code for it:

Screen Shot 2020-09-25 at 9 16 51 AM

Tasks

I want to test the first row in the table is the one that I have added. so I need to:

1. Locate the table element

Find out the table tag and the relevant attributes for it from this bit of code. <table _ngcontent-c8="" class="table table-bordered table-striped table-hover table-light" matsort="" matsortactive="dateCreated" matsortdirection="desc" matsortdisableclear="" matsortstart="desc">

To locate the table element I have to get the table ID, name or class, here I have table class name which is "table table-bordered table-striped table-hover table-light" using XPath. driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']"));

2. Locate row 1, column 1, row 1 column 2... and row 1 column 6

To locate row1, column1, we only need to add /tbody/tr[1]/td[1] after the table locator. With the same idea, if I want to get row1 column6, I would add /tbody/tr[1]/td[6] Simple as it is, isn't it!!!

3. Do the assertion

Assert.AreEqual(expected_value, actual_valaue);

Here are the codes for this test:

            //verify the ivoice is the new-added one
            string invoice_number_value = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[1]")).Text;
            Assert.AreEqual(invoice_number, invoice_number_value);
            //Get invoice creation date
            string invoice_creation_date = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[2]")).Text;
            Assert.AreEqual(currentdate, invoice_creation_date);
            //Get invoice client on the list
            string invoice_client = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[3]")).Text;
            Assert.AreEqual(clientname, invoice_client);
            //Get invoice amount on the list
            string invoice_total_amount = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[4]")).Text;
            string newtotal1_format = "$" + newtotal1;
            Assert.AreEqual(newtotal1_format, invoice_total_amount);
            //Get omvoice due date on the list
            //string list_duedate = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[5]")).Text;
            //Assert.AreEqual(duedate, list_duedate);
            //Get invoice status on the list
            string list_invoice_status = driver.FindElement(By.XPath("//*[@class='table table-bordered table-striped table-hover table-light']/tbody/tr[1]/td[6]")).Text;
            Assert.AreEqual("Draft", list_invoice_status);
            Thread.Sleep(2000);

Thanks and comments.