There are 5 actions in autocomplete,
The input field for us to enter criteria is named "q", so you can use By.name("q") to find it and type the characters of Oracle one by one into it and wait for "Oracle" appears in the selection area and click it.
It is difficult to locate the suggestion items since there is no obvious selection criteria to find them.
You can use Firebug to find its absolute xpath, to get some idea but it can be used in the tests since the structure of the page may change and it will not work,
/html/body/table/tbody/tr/td[2]/table/tbody/tr/td/div/table/tbody/tr/td/span
If you are not familiar with xpath, you can use complex search to find a container first and from the container, find the element. This autocomplete suggestion is inside a table with class "gssb_c", so we can findElement of the table first, and the findElements of all the span elements inside that table and find the one with the text "oracle" you try to type in,
// ☹ this is a bad example, please don't follow the style. try { WebElement table = webDriver.findElement(By.className("gssb_c")); List<WebElement> spans = table.findElements(By.tagName("span")); WebElement oracle = find(spans, new Predicate<WebElement>() { @Override public boolean apply(WebElement span) { return span.getText().equals("oracle"); } }); oracle.click(); } catch (NoSuchElementException e) { e.printStackTrace(); }
This is not the complete logic, it is just the part to find the "oracle" from the suggestion list, it is already very verbose.
Selenium capsules, a new Selenium framework, uses a locator pattern to abstract the element locating logic to make the test code cleaner, in the autoCompleteUsingLocator test from following code block.
Note, () -> className("gssb_c") is a lambda expression introduced in Java 8.
// ☺ This is a good example. new ElementTryLocator<AbstractPage>(() -> className("gssb_c")) .and(new ElementsLocator<>(SPAN)) .and(new FirstMatch<>(TEXT.and(new IsStringEqual("oracle")))));
It is a little bit cleaner, but still very complex.
After rewriting the search using By xpath, it only has one findElement method call, which is much better than the navigational locating strategy, so you can greatly simplify the test if you know how to use xpath.
// ☹ this is a bad example, please don't follow the style. try { WebElement oracle = webDriver.findElement( By.xpath("//table[contains(concat(' ', @class, ' '), 'gssb_c')]/descendant::span[text()='oracle']"); oracle.click(); } catch (NoSuchElementException e) { e.printStackTrace(); }
But it is still not clean if the By.xpath call appears multiple times in different tests,
By.xpath("//table[contains(concat(' ', @class, ' '), 'gssb_c')]/descendant::span[text()='oracle']");
By using Selenium Capsules framework, you can actually put this xpath string literal inside an enum and use the enum in the tests,
// ☺ This is a good example. import org.openqa.selenium.By; import java.util.function.Supplier; import static org.openqa.selenium.By.xpath; public enum Xpath implements Supplier<By> { DIV_CONTAINER_ID("//div[@id='container']"), ORACLE_AUTOCOMPLETE("//table[contains(concat(' ', @class, ' '), 'gssb_c')]/descendant::span[text()='oracle']"), QUANTITY("//div[@id='ys_cartInfo']/descendant::input[@name='cartDS.shoppingcart_ROW0_m_orderItemVector_ROW0_m_quantity']"); private final By by; private Xpath(String id) { this.by = xpath(id); } @Override public By get() { return by; } @Override public String toString() { return by.toString(); } }
Now the test can be rewritten as simple as one line of code,
// ☺ This is a good example. @Test public void autoCompleteUsingXpath() { googlePage.autocomplete(Q, "oracle", new ElementTryLocator<>(ORACLE_AUTOCOMPLETE)); }
Even you don't want to use a framework, putting locating strategy in enum can still simplify code, unlike the above code, you can just use ORACLE_AUTOCOMPLETE as a parameter, you need to call get() method to get a By object, but it is still cleaner than using xpath string literals directly in the code,
// ☺ This is an OK example. try { WebElement oracle = webDriver.findElement(ORACLE_AUTOCOMPLETE.get()); oracle.click(); } catch (NoSuchElementException e) { e.printStackTrace(); }
versus original,
// ☹ this is a bad example, please don't follow the style. try { WebElement oracle = webDriver.findElement( By.xpath("//table[contains(concat(' ', @class, ' '), 'gssb_c')]/descendant::span[text()='oracle']"); oracle.click(); } catch (NoSuchElementException e) { e.printStackTrace(); }
Now let us have a comparison between the tests written with and without using framework,
1. autoCompeleteUsingSelenium doesn't use framework, it uses Selenium directly.
2. autoCompleteUsingXpath uses framework,
Cohesively, all By selectors can be organized into one package, selectors to encourage people reuse existing definitions, the name of that package should be bysuppliers, I found out selectors is a more meaningful name since that's the purpose of those classes.
By Selector Suppliers and all its member classes are enum types implementing Supplier
Nice article.
ReplyDeleteThat was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
ReplyDeleteSelenium Training in Chennai
Selenium Training in Chennai
ReplyDeleteSelenium Training
iOS Training in Chennai
iOS Training Institutes in Chennai
PHP Training in Chennai
PHP Course in Chennai
good work done and keep update more.i like your information's and
ReplyDeletethat is very much useful for readers.
python tutorial in bangalore
python certification in bangalore
Python Training in Guindy
Excellent post! keep sharing such a post
ReplyDeleteArticle submission sites
Technology
The blog is well written. Thanks for your information.,java is fast, secure and reliable platform. there are lots of application and websites works under java and it is platform independant
ReplyDeleteBest Institute for JAVA
Best JAVA Training
JAVA Programming Certification Course
Best JAVA Certification
Best JAVA Training
Thanks for your efforts in sharing this information in detail. This was very helpful to me. kindly keep continuing the great work.
ReplyDeleteWeb DesigningTraining in Chennai Velachery
Web Designing Training in Saidapet
Web Designing course in velachery
Web Designing Training in Kandanchavadi
Web Designing Training in Kelambakkam
Web Designing Course in Tambaram
Thank you for sharing such a nice post. It shows your deep knowledge on the subject. Pls keep updating.
ReplyDeleteHadoop Administration Training in Chennai
Hadoop Administration Course in Chennai
Hadoop Administration Training
Big Data Administrator Training
Hadoop Administration Course
Big Data Administration Course in Chennai
It’s a great post. Keep sharing such kind of worthy information.
ReplyDeleteManual Testing Training in Chennai | Manual Testing Training | Manual Testing Training institute in Chennai | Manual Testing Training in Adyar | Manual Testing Training in Tambaram | Manual Testing Training in Velachery
Thanks for your efforts in sharing this information in detail. This was very helpful to me. kindly keep continuing the great work.
ReplyDeleteTOEFL Coaching in omr
TOEFL Training in Perungudi
TOEFL Classes near Thoraipakkam
TOEFL Centres in Shollinganallur
TOEFL Training at Padur
TOEFL Classes in OMR
TOEFL Classes near me
Nice article. I was really impressed by seeing this article, it was very interesting and it is very useful for me.
ReplyDeleteEnglish Speaking Institute in Mumbai
Spoken English classes in Chennai
Best English Classes in Mumbai
Best English Speaking Course in Mumbai
Best Spoken English Classes in Mumbai
I was barely amazed at how you had written this content. Please keep posting.
ReplyDeleteCCNA Training in Saidapet
CCNA Training in Chennai Kodambakkam
CCNA Training in Chennai
CCNA Training in Nungambakkam
CCNA Course in Kodambakkam
Useful content, I have bookmarked this page for my future reference.
ReplyDeleteAngularjs Training in Chennai
Angularjs Training near me
Angularjs course in Chennai
Angularjs Training
RPA Training in Chennai
DevOps Training in Chennai
Blue Prism Training in Chennai
Thanks for sharing the amazing post.It is very much informative. I am very eager to read your upcoming post.
ReplyDeletePrimavera Training in Chennai
Primavera Course in Chennai
Primavera Software Training in Chennai
Best Primavera Training in Chennai
Primavera p6 Training in Chennai
Primavera Coaching in Chennai
Amazing Post. I am very much impressed with your choice of words. The content showcases your in-depth knowledge in this subject. Thanks for sharing.
ReplyDeleteSocial Media Marketing Courses in Chennai
Social Media Marketing Training in Chennai
Social Media Training in Chennai
Social Media Marketing Training
Social Media Marketing Courses
Social Media Training
Social Media Marketing Training
Social Media Courses
ReplyDeleteThanks for sharing the fantabulous post. It gives immense pleasure to read your article. Your post is very thought provoking.
Pega training in chennai
Pega course in chennai
Pega training institutes in chennai
Pega course
Pega training
Pega certification training
Pega developer training
You are doing a great job. I would like to appreciate your work for good accuracy
ReplyDeleteRegards,
PHP Training in Chennai | PHP Course in Chennai
Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog.
ReplyDeleteKeep update your blog.
Technology
technocrawler
imo download for pc
ReplyDeletenice...
ReplyDeleteinplant training in chennai
inplant training in chennai for it
Bermuda web hosting
Botswana hosting
armenia web hosting
dominican republic web hosting
iran hosting
palestinian territory web hosting
iceland web hosting
Thanks for sharing this wonderful information. It is just awesome.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
Damien Grant
ReplyDeleteDamien Grant
Damien Grant
Damien Grant
Damien Grant
Damien Grant
Damien Grant
Damien Grant
Nice code..
ReplyDeleteSCCM training
SCOM training
SSAS training
SSIS training
SSRS training
This comment has been removed by the author.
ReplyDelete