0

OrderDetailPage

I have a webpage where Order generated gets displayed in list. I have constraint for the Order details section which has the newly generated Order ID and verify for the Order date and Order status.

Inspect details

<div class="section order-item">
 <div class="title">
   <strong> Order Number: 1772269 </strong>
 </div>
 <ul class="info">
   <li>Order status: Pending</li>
   <li>Order Date: 8/16/2024 1:52:45 AM</li>
   <li>Order Total: 807.00</li>
 </ul>
</div>

In the above html tree, I need to constraint the section order-item which has order number 1772269 and then check for the order status as Pending and Order date as current date. Note, Order number gets generated during runtime.

Using driver.findElement() function, I am able to pass the Order number dynamically during runtime and validate for Order Number.

WebElement orderNumber = driver.findElement(By.xpath("//div[@class='section order-item']//div[@class='title']//strong[contains(text(),'"+orderNum+"')]"));

Query 1: How to implement this in PageFactory model. Because, in page factory model if I give the same format it is not accepting the dynamic value.

@FindBy(xpath="//div[@class='section order-item']//div[@class='title']//strong[contains(text(),'Order Number: "+orderNum+"')]") WebElement orderNumber;

Query 2: I am able to traverse until OrderNumber using Xpath //div[@class='section order-item']//div[@class='title']//strong[contains(text(),'Order Number: 1772269')]

But how to reach orderStatus and Order date. If I try to enter //div[@class='section order-item']//div[@class='title']//strong[contains(text(),'Order Number: 1772269')]//ul it is not reaching the path.

Inspect Order Number

Inspect Order Status

1 Answer 1

0

I think you haven't understood the meaning of // in XPath.

I recommend you read the Location Paths section in the XPath 1.0 spec so that you know what the different "axes" are in XPath path expressions, and understand the abbreviated forms such as that a/b is short for a/child::b, a/@b means a/attribute::b, and a//b means a/descendant-or-self::node()/b (or more concisely a/descendant::b).

NB the // idiom is useful for "skipping over" a number of elements in a deeply nested hierarchy, i.e. it performs a "deep search" within a portion of the XML hierarchy. But you don't need it, apart from at the start of the expression, where the initial // will search from the root of the document for the div[@class='section order-item'].

You don't need to search for the div[@class='title'] within the descendants of the //div[@class='section order-item'], because the two div elements are parent and child, and you can simply use /, and the same applies to the nested div and its strong child element. By the way, div/strong means div/child::strong, but you don't need to explicitly include the child:: axis specifier because the child axis is the default. e.g.

//div[@class='section order-item']/div[@class='title']/strong

Secondly, the ul is not a descendant of the strong element, so a path expression that ends in strong//ul is never going to return anything. The ul is a child of the //div[@class='section order-item'], and the li elements are children of the ul i.e. the three li elements can be addressed with the following location path:

//div[@class='section order-item']/ul/li

Combining these XPath expressions, we can select the outer div which has the particular order number we want, a status of "Pending" and a particular date, with an expression like this:

//div
   [@class='section order-item']
   [div[@class='title']/strong[contains(., '1772269')]]
   [ul/li = 'Order status: Pending']
   [ul/li
      [starts-with(., 'Order Date')]
      [contains(., ' 8/16/2024')]
   ]

(I find it very helpful to use indentation, as above, when writing and testing an XPath query; if you generally start a new line with an added indent at each / step in your path or [ beginning a new predicate, then your XPath expression ends up with a similar shape to the structure of the XML document you're querying. That helps to ensure you have the boolean predicates lined up against the nodes they apply to, and of course it also helps you with basic syntax checking like matching [ and ] characters, etc.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.