开发者

Selenium: How do I assert that a certain element is present in a certain cell of a certain table?

开发者 https://www.devze.com 2023-03-31 12:15 出处:网络
I have a table on my page which is supposed to contain a certain element. I can identify the table by its name (it has a unique name), and I can also identify the element easily. I would like to asser

I have a table on my page which is supposed to contain a certain element. I can identify the table by its name (it has a unique name), and I can also identify the element easily. I would like to assert that the element is present on row r, column c of the table. What is the cleanest way of doing it using Selenium commands?

Remarks:

  • I don't want to use more than the table name in order to locate it (I don't want all the div\div\table\div\tbody\td\tr[r]\td[c] in the code).
  • I'm using Selenium within PHPUnit. Hence, I can use PHP logic for the task, though I don't want any complex logic for such a simple task.

Clarification:

If the element in the cell is just plain text, then I can retrieve that text like this:

$this->getText("xpath=//table[@name='tableName']//tr[".$r."]//td[".$c."]"); (PHP)

But what if the cell has an element which is not just plain t开发者_StackOverflowext? What if the element is a link (link=anchor) or a button (//button[@type='button']) or an image or something more complex?

I need to assert that an element specified by a locator of that element resides in a given cell.


Sounds like you want isElementPresent(...locator of element...). For example:

$cell = "//table[@name='tableName']//tr[".$r."]/td[".$c."]";
$foundLink = $this->isElementPresent("xpath=".$cell."/a[.='".linktext."']");
$foundButton = $this->isElementPresent("xpath=".$cell."/button[@type='button']");
$foundImage = $this->isElementPresent("xpath=".$cell."/img[ends-with(@src='pretty-pony.gif')]");

isElementPresent() returns true if so, false if not.


You could try Selenium's getXpathCount

$this->("xpath=//table[@name='tableName']//tr[".$r."]//td[".$c."]//TAG");
This will return the number of matches the xpath gets. in your case, zero would mean a fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消