Skip to main content

Posts

Wildcard in assertions error messages

Assume that you want to compare whether 2 numbers are equal or not. Most of us would do it like: @Test public void testCompare() { int x = 92; int y = 99; Assert.assertTrue("x is not equal to y", x == y); } And as we can see that these numbers aren't equal, so we'll get the error in this case. But it is not clear at all why the assertion fails as the values of x and y are not included in the failure message. FAILED: testCompare AssertionError: x is not equal to y Is there a way we can improve the message so that we can get the complete info from the error message itself? Yes, how about using wildcards in the error message: @Test public void testCompare() { int x = 92; int y = 99; String notEqualMsg = "%d is not equal to %d"; Assert.assertTrue(String.format(notEqualMsg, x, y), x == y); } FAILED: testCompare AssertionError: 92 is not equal to 99 Tada...problem solved!!! hashtag # easier hashtag # betterway hashtag # sele

Have you heard of "ngrok"?

It's a very cool tool that lets you expose a local webserver to the outside world (internet)- in short, it creates a secure tunnel on your local machine along with a public URL that you can use or give to your colleagues, friends, etc. for browsing or testing your local site. Go through this to know more about it: https://ngrok.com/docs I have used this where I wanted to expose my local running Jenkins to git for the webhook part.

Have you ever encountered this type of Exception where it says Element can't be clickable?

Element can't be clickable exception in Selenium : org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (660, 823) (Session info: chrome=76.0.3809.132) (Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=XXX.XXXXX) Command duration or timeout: 65 milliseconds Most of the time. we go and first check manually to see that the element is visible and clickable. And then, you check the locator that you have selected is correct and the element also shows in the browser DOM. Then why is it failing? One of the reasons may be that the element is not in the viewport. So, how we can resolve this? Scroll to the page so that the element becomes visible, then hopefully the exception should not occur. JavascriptExecutor jsExec = (JavascriptExecutor)driver; jsExec.executeScript("window.scrollTo(0, document.body.scrollHeight)"); or WebElement element = document.getElementById("submit"); Javascript

ExpectedConditions in Selenium

How do you usually check that a page is displayed in selenium or not? Most of us would check that the Page Title and the URL is correct: String expUrl = “ https://testersdigest.blogspot.com ”; String expTitle = “My Testing Stories”; WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.titleIs(expTitle)); wait.until(ExpectedConditions.urlToBe(expUrl)); First-line here will validate the title check, next one for the URL. But, using the ExpectedConditions class which allows combining the 2 lines we can do it more cleaner: wait.until(ExpectedConditions.and(ExpectedConditions.urlToBe(expUrl),ExpectedConditions.titleIs(expTitle))); //ExpectedConditions.and() waits until multiple conditions are true. Not only and() but we also we have or() and not() for ExpectedConditions. -- wait.until(ExpectedConditions.or(ExpectedConditions.urlToBe(expUrl),ExpectedConditions.titleIs(expTitle))); //waits until at

Why all API enthusiasts should know about GraphQL?

With GraphQL, what you queried is what you get, nothing more and nothing less. It is easy to use as it has a JSON like syntax and also provides lots of performance benefits. Here's a query for the Star Wars API: https://swapi.apis.guru/graphiql query{ allFilms { films { id title episodeID } } } This query returns the id, title, and episodeID of the 6 original Star Wars movies. Now consider that there are 2 vendors that are consuming this API and one of them wants id, title, and episodeID and the other one wants the only id, and title but NOT the episodeID. Now without GraphQL, we would have created 2 versions of this API i.e. the first one with the id, title, and episodeID in the response and the second version giving a response with id and title only. Isn't it a headache to maintain so many versions if you have different requests from various vendors that are consuming your APIs? That's where a GraphQL is a real lifesaver. Please check graphQLComplete method shown

RelativeBy Locators in Selenium

Have you started using RelativeBy Locators (Friendly locators) in Selenium 4 (alpha-3) yet or not? If not, then you should give it a try. These new locator methods help you find elements based on their visual location relative to other elements in the DOM. As of now, it supports with "withTagName" attribute and allows the following: - above - below - near - toleftOf - toRightOf  It supports both By and WebElement. Sample syntax:  WebElement unameLabel = driver.findElement(By.cssSelector("label[id=''uname]")); WebElement username =  driver.findElement(withTagName("input").toRightOf(unameLabel)); or you can use: WebElement username =  driver.findElement(withTagName("input").toRightOf(driver.findElement(By.cssSelector("label[id=''uname]")));

Catlight (clean and simple build notification tool)

If you would like to use a clean and simple build notification tool that integrates with all major CI/CD tools like Jenkins, TFS, Team City, etc.- you can give a shot to https://catlight.io/ (Community version). It can be handy if you don't have integration with other communicators like Slack, Teams, etc. hashtag # cd hashtag # jenkins hashtag # catlight hashtag # notification hashtag # tfs hashtag # integration hashtag # devops hashtag # automation hashtag # tools