Selenium Cheatsheet
Selenium Cheatsheet
CSS selector for selecting all childs of an element : (descendent combinator i.e. space character)
div#idname div[class='newclassname'] : selects all the child of the parent element
CSS selector to select the next sibiling : (+ operator : adjacent sibling combinator)
tagname1[attributename='attributevalue']+tagname2[attributename='attributeValue']
the tagname2 is only selected if it is immediately follows (immediate sibling) of tagname1 and both are the childs of the same
CSS Psedu classes
It is a keyword added to the CSS selector that specified the special state of the weblement.
1. first-child : returns first element from a group of siblings
tagname[attributename='attributeValue']>:first-child or tagname[attributename='attributeValue'] :first-child
2. last-child : returns last element from a group of siblings
tagname[attributename='attributeValue']>:last-child
3. nth-child() : returns nth child element from a group of siblings counting from first element
tagname[attributename='attributeValue']>:nth-child(3)
4. nth-last-child() : returns nth element from a group of siblings counting from the last element
tagname[attributename='attributeValue']>:nth-last-child(4)
ex -> >:first-child or :first-child or tagname:first-child or *:nth-child
5. first-of-type
tagname1[attributename='attributeValue']>tagname2:first-of-type -> will select first child of type tagname2 under the paren
6. last-of-type
tagname1[attributename='attributeValue']>tagname2:last-of-type ->will select last child of type tagname2 under the parent t
7.nth-of-type()
tagname1[attributename='attributeValue']>tagname2:nth-of-type ->will select last child of type tagname2 under the parent ta
2. Explicit : to define wait for a particular element. used when an exclusive wait time is needed for any specific elements but n
WebDriverWait w = new WebDriverWait(driver, 5);
w.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//span[@class='promoInfo']")));
3. Fluent : to define wait for an element with regular polling. IT is a type of explicit wait. Explicit wait is of two type :
a) WebDriverWait ; - continuously checks the dom for the element till the element is found.(approx- every milisecond)
b) Fluent wait :- checks the dom for the element after regular intervals(polling) till timeout or till object is found. Unlike, webd
to build customized wait methods based on condition. both WebDriverWait and FluentWait implement Wait interface
});
scope of selenium Webdriver object can be limited by making a webelement and calling the driver methods on the element. F
driver.findElements(By.xpath("//a")).size() //will give the no of links on the entire page.
using Assert.assertTrue or similar Assert class methods, stops the test then and there. So instead, SoftAssert class can be used
failures and then at the end fail the test by showing all the errors
SoftAssert a =new SoftAssert();
a.AssertTrue(i<0,"i should be less than zero");
a.assertAll(); // to be used after all assertions to throw the assertion errors.