<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Masihur Rahman Maruf</title>
    <description>The latest articles on DEV Community by Masihur Rahman Maruf (@masihurmaruf).</description>
    <link>https://dev.to/masihurmaruf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F245036%2Fcb63b2c5-45dd-442d-bc23-c237fb3d7f33.jpeg</url>
      <title>DEV Community: Masihur Rahman Maruf</title>
      <link>https://dev.to/masihurmaruf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/masihurmaruf"/>
    <language>en</language>
    <item>
      <title>Locator Strategy: CSS Selector</title>
      <dc:creator>Masihur Rahman Maruf</dc:creator>
      <pubDate>Thu, 10 Oct 2019 13:14:41 +0000</pubDate>
      <link>https://dev.to/masihurmaruf/locator-strategy-css-selector-36k3</link>
      <guid>https://dev.to/masihurmaruf/locator-strategy-css-selector-36k3</guid>
      <description>&lt;h1&gt;
  
  
  Preface:
&lt;/h1&gt;

&lt;p&gt;In my previous blog I discussed about XPath. In this blog I will try to focus on some most important concepts in CSS Selector. I will strongly recommend to read the Locator Strategy: XPath before reading this blog.&lt;/p&gt;

&lt;p&gt;CSS Selector should be preferred when we don’t have any suitable Id or name. Selectors are patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in an XML document. For more information visit W3C Recommendations.&lt;br&gt;
Syntax:&lt;/p&gt;

&lt;h2&gt;
  
  
  Direct Child:
&lt;/h2&gt;

&lt;p&gt;In CSS, “&amp;gt;” is used to define the direct child relationship between the elements. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;html&amp;gt;body&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Body tag is the direct child of html tag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Descendent(sub-child):
&lt;/h2&gt;

&lt;p&gt;If an element is the any child or grand child of the current element, but not a direct child in CSS it is defined by whitespace.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;div img&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That means, img tag is a descendant of div tag, but not a direct child.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding Element using Id and Class attributes:
&lt;/h2&gt;

&lt;p&gt;For Id attribute in an element CSS has a special symbol “#”. Unlike XPath you can find element directly using Id.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#products&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will find the element that has id ‘products’.&lt;/p&gt;

&lt;p&gt;Like Id you can identify element using class name. For that you need to use “.”.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.setter&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can use above selector to find element that has class name setter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding elements using other attributes:
&lt;/h2&gt;

&lt;p&gt;CSS support finding elements using attribute other then Id and class. Have a look on the following example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img[title='English']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finding an image element using attribute title.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding elements using multiple attributes:
&lt;/h2&gt;

&lt;p&gt;You can find element using multiple attributes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img[title='English'][alt='United States']&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use of Wildcards:
&lt;/h2&gt;

&lt;p&gt;CSS supports use of wildcard characters like “^”, “$” and “*”.&lt;/p&gt;

&lt;p&gt;You can use “^” with attribute name to simulate ‘starts-with’. Let’s see an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img[title^='En']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will find any image tag that has an attribute named ‘title’ with value starts with ‘En’.&lt;/p&gt;

&lt;p&gt;Let’s see the use of “$”.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img[title$='sh']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It simulates ‘ends-with’. Will find an image tag that ends with value ‘sh’.&lt;/p&gt;

&lt;p&gt;“*” is used to find any specific pattern in the value of attributes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;img[id*='n_U']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here it will search for image tag with a pattern ‘n_U’.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;In this blog, I focused on the basic usage of CSS Selectors. Next time I will explain some of the advanced topics of CSS Selectors. Till then practice…&lt;/p&gt;

</description>
      <category>css</category>
      <category>testing</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Locator Strategy: XPath</title>
      <dc:creator>Masihur Rahman Maruf</dc:creator>
      <pubDate>Thu, 10 Oct 2019 13:09:23 +0000</pubDate>
      <link>https://dev.to/masihurmaruf/locator-strategy-xpath-54p7</link>
      <guid>https://dev.to/masihurmaruf/locator-strategy-xpath-54p7</guid>
      <description>&lt;h1&gt;
  
  
  Preface:
&lt;/h1&gt;

&lt;p&gt;One of the most challenging task in automating Web application using Selenium WebDriver is finding Web element. Selenium WebDriver supports a number of locator strategies-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Id&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;XPATH&lt;/li&gt;
&lt;li&gt;Class Name&lt;/li&gt;
&lt;li&gt;Name&lt;/li&gt;
&lt;li&gt;Tag Name&lt;/li&gt;
&lt;li&gt;Links Text&lt;/li&gt;
&lt;li&gt;Partial Links Text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Among all the locator strategies &lt;strong&gt;Id&lt;/strong&gt; is the best locator, as a standard, Id is unique in a web page. But in real life nothing is perfect. In real life scenario you will hardly get Id in every element. Here comes the need for the rest of the locator strategies.&lt;/p&gt;

&lt;p&gt;Keeping this in mind I am going to write a series of blogs regarding locator strategies. In this blog I will try to focus on XPath Basics.&lt;/p&gt;

&lt;h1&gt;
  
  
  XPath:
&lt;/h1&gt;

&lt;p&gt;XPath, the XML path language, is a query language for selecting nodes from an XML document. The XPath language is based on a tree representation(hierarchical) of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria. XPath has been adopted by a number of XML processing libraries and tools, many of which also offer CSS Selectors, another W3C standard, as a simpler alternative to XPath.&lt;/p&gt;

&lt;p&gt;Locating elements with XPath works very well with a lot of flexibility. However, this is the least preferable locator strategy due its slow performance. One of the important differences between XPath and CSS is, with XPath we can search elements backward or forward in the DOM hierarchy while CSS works only in a forward direction. This means that with XPath we can locate a parent element using a child element. Remember XPath is a case sensitive language.&lt;/p&gt;

&lt;p&gt;All the major browsers support XPath as HTML pages are represented as XHTML documents in DOM and used by Selenium WebDriver for locating elements. There are a number of tools you can use to find out XPath. For Firefox browser Firebug and XPath Checker Add-on and for Chrome XPath Helper and Toggle XPather.&lt;br&gt;
XPath Expression:&lt;br&gt;&lt;br&gt;
XPath expression generally defines a pattern in order to select a set of nodes. XPath uses path expression to select nodes or list of nodes from an XML/html document. This path can be of two type:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Absolute Path&lt;/li&gt;
&lt;li&gt;Relative Path&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Absolute path means to specify every single node to select a node inside a hierarchical document. It always starts from root node. Here is an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/html/body/div[2]/div/div[4]/div/ul/li[3]/a/span&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It is relatively faster then Relative path. But the XPath expression may be very lengthy. More over each of those nested relationships will need to be present 100% of the time, or the locator will not work. For these reasons use of Relative XPath expressions are encouraged.&lt;/p&gt;

&lt;p&gt;In Relative XPath expression you can start from the node of your choice. The above absolute path can be written in the following relative path:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//div[@id='navigation']/ul/li[3]/a/span&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this blog, my prime focus will be to demonstrate the Relative XPath.&lt;br&gt;
Expression: “/” &amp;amp; “//”&lt;br&gt;
You may notice from above two example that I sometime used “/” and sometimes “//”. This is a very important point to understand. A single slash ‘/’ anywhere in XPath signifies to look for the element immediately inside its parent element. A double slash ‘//’ signifies to look for any child or any grand-child (descendant) element inside the parent element. In the following two example it will be more clear.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//div[@id='products']/a/img&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Look carefully in the above example, here I am trying to find an image tag from html document, which is the child of tag ‘a’. And tag a is the child of div tag. The same image tag can be found by:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//div[@id='products']//img&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here I am trying to find an image tag which is the descendant of tag div.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expression: “.” &amp;amp; “..”
&lt;/h2&gt;

&lt;p&gt;You may have noticed that in every example I have used “.”. This is used to specify current node in XPath.&lt;br&gt;
“..” is used to move one step up in the document tree, that is it will find the parent of current node. An example will make it more clear:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//div[@id='products']/..&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This expression will find the parent node of the current node.&lt;/p&gt;

&lt;h3&gt;
  
  
  Expression: “@”
&lt;/h3&gt;

&lt;p&gt;“@” is used to select attribute in the document tree.&lt;br&gt;
In the above example I specify attribute id with “@” and the value products inside ‘ ’.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expression: “*”
&lt;/h2&gt;

&lt;p&gt;XPath support wildcard like “*”. Sometimes it is of much use to find a Relative path. Let’s see an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//*[text()='Home']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This wildcard can be used in place of attribute too.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//a[@*='Home']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this case I am trying to find an element which has an attribute with value ‘home’. In this case I don’t care about the attribute name. You will not use it very often. But its good keep it as an option.&lt;br&gt;
Here in this section I am going to illustrate to most handy trick of XPath. Most of the times you will face issues when the locator’s properties are dynamically generated. And now a days petty much all the web application follows this strategy. Here comes the real challenge to deal with all the issue. Following section will give you concept about how to deal with such situations.&lt;/p&gt;

&lt;p&gt;Read the following section carefully and try to understand and implement these tricks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expression using keyword “contains”:
&lt;/h2&gt;

&lt;p&gt;“contains()” is a function that take two arguments, attribute name and attribute value. Let’s see an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//div[contains(@id, 'search')]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here I am finding a div tag which contains an attribute named ‘id’, in which the value contains ‘search’. It doesn’t need to be exactly the ‘search’ but the pattern needs to match. Function ‘contains()’ match attribute name with value by matching pattern.&lt;br&gt;
Here is another use of function ‘contains()’:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//span[contains(text(), 'Hot')]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This expression finds a span tag which contains ‘Hot’ as text in it.&lt;br&gt;
Expression using keyword “text()”:&lt;br&gt;
In previous example you saw, I used function ‘text()’. Let’s have a deeper look into function ‘text()’. It will find out a specific tag which has specific text in it. Here is an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//span[text()= 'Home']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As I said this expression will find a span tag with text ‘Home’.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expression using keyword “starts-with()”:
&lt;/h2&gt;

&lt;p&gt;Like function ‘contains()’, ‘starts-with()’ also take two arguments, attribute name and attribute value. Let’s look it in an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//div[starts-with(@class, 'width')]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here I am trying to find a div tag where there is an attribute ‘class’ that starts with pattern ‘width’. This function also find element by matching pattern.&lt;br&gt;
Another example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.//span[starts-with(text(), 'Gift')]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As you saw earlier in ‘contains()’ function I used ‘text()’ function to find text inside that tag. That can be also done with function ‘starts-with()’ .&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion:
&lt;/h1&gt;

&lt;p&gt;In this blog, I focused on a number of the tricks and techniques that you will use daily as a Test Automation Engineer. There is no end of learning. You can learn more about XPath. Next time I will try to focus on some advance topic in XPath. Till then Practice…&lt;/p&gt;

</description>
      <category>xpath</category>
      <category>testing</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
