Find.By - finding & verifying locators
I will start this article with the fact that I work with QA Automationg and that I like to automate all routine activities. So recently, for me, writing, editing and checking locators for elements on a page has become such.
Usually this process looks like this: I write an xpath expression in chrome or firepath, then copy it and add the attribute to the element in C # code. But locators often need to be fixed or just check which element it points to. And even such a simple change in the predicate as [@ id = 'myId'] to [contains (@id = 'Id')]ends with the test crashing at runtime because I wrote '=' instead of ',' and was too lazy to check the changes. In general, there are too many actions with copying, pasting, switching between windows and the like for such a simple task. I decided to write a plugin for ReSharper, which would highlight my element in the browser by Alt + Enter.
Write plugins for ReSharper and Chrome, which together would solve two simple tasks:
With the first task, everything turned out to be quite simple. I decided not to write my parser and validator for xpath, but simply used the XmlDocument class from the .NET library:
The second part of the question was implemented as follows:
To search for elements in dom using XPath, I use the jquery.xpath.js library .
Since I had never written plugins for chrome before, the most difficult thing for me was sending requests from the https site to http localhost. Google Chrome blocks all such requests from the web page. It took me a short time to find a solution, but it turned out to be very simple and, probably, even logical: you can make http requests from background.js, and then transfer the result to the page, which I did.
We are writing the simplest plugin for ReSharper - an article that helped start writing for ReSharper
Find.By chrome - a plugin for Google Chrome
Find.By resharper - a plugin for ReSharper
Find.By - github project repository
Usually this process looks like this: I write an xpath expression in chrome or firepath, then copy it and add the attribute to the element in C # code. But locators often need to be fixed or just check which element it points to. And even such a simple change in the predicate as [@ id = 'myId'] to [contains (@id = 'Id')]ends with the test crashing at runtime because I wrote '=' instead of ',' and was too lazy to check the changes. In general, there are too many actions with copying, pasting, switching between windows and the like for such a simple task. I decided to write a plugin for ReSharper, which would highlight my element in the browser by Alt + Enter.
Formulation of the problem
Write plugins for ReSharper and Chrome, which together would solve two simple tasks:
- Static analyzer XPath expressions;
- Highlighting any selenium locator in the browser by Alt + Enter.
Decision
With the first task, everything turned out to be quite simple. I decided not to write my parser and validator for xpath, but simply used the XmlDocument class from the .NET library:
XmlDocument document = new XmlDocument();
var navigator = document.CreateNavigator();
try
{
navigator.Compile("xpath expression");
//Если не получили исключение, то выражение валидно
}
catch (XPathException exception)
{
//Выражение не валидно
}
The second part of the question was implemented as follows:
- ReSharper plugin raises TcpServer for localhost
- The Chrome plugin constantly asks him “Is there anything to highlight?”
- The list of what would highlight empty until the user press Alt + Enter and select the Highlight element
To search for elements in dom using XPath, I use the jquery.xpath.js library .
Since I had never written plugins for chrome before, the most difficult thing for me was sending requests from the https site to http localhost. Google Chrome blocks all such requests from the web page. It took me a short time to find a solution, but it turned out to be very simple and, probably, even logical: you can make http requests from background.js, and then transfer the result to the page, which I did.
Plugin operation in action
References
We are writing the simplest plugin for ReSharper - an article that helped start writing for ReSharper
Find.By chrome - a plugin for Google Chrome
Find.By resharper - a plugin for ReSharper
Find.By - github project repository