Search This Blog

Breaking

Sunday, 31 October 2021

October 31, 2021

[Fix] [Windows]Text is coming in incorrect encoding in Jenkin and in local machine

 Recent ally, I encountered one issue where one of the multilingual texts








was coming differently in my local environment(IDE). It was a pretty annoying issue because the string was coming with special characters when I was running my program, while when I tried to replicate it in a different project, the same was working fine.

I posted the question in StackOverflow too but could not find a better solution for it.

To fix this issue, I used apache common lang3 Stringutil escape until method. This method converted the special characters to HTML. Now, I thought to put a regex to remove the HTML. But with my bad, it didn't work.

So, after spending a lot of time, I figured that this is happening because of encoding. So I checked my file encoding.

In Intelij, we can check encoding by: ctril+shift+d -> Editor->File Encoding.

There I found that the file encoding was set as "Windows 1252". 

I changed it to UTF-8. Later, I change all individual file encoding also to UTF-8.

This fixed the issue in my local machine.

The same issue when I got in Jenkin machine, I fixed the encoding from the pom.xml file.

Here is my question from StackOverflow.

 

Thursday, 28 October 2021

October 28, 2021

[Fix] Selenium click is not working in Jenkin while working fine in Local

 If the click() in your selenium UI script is failing in Jenkin machine, but in local, it is working fine, then you can use below tricks:

1. Replace selenium click() to JavaScript Executor click()

JavascriptExecutor javascriptExecutor =  (JavascriptExecutor) driver;

javascriptExecutor.executeScript("arguments[0].click();", element);





2. Scroll till that element first, and then perform click.

JavascriptExecutor javascriptExecutor =  (JavascriptExecutor) driver;

javascriptExecutor.executeScript("arguments[0].scrollIntoView();", element);

3. Use double click

element.doubleClick()

4. Use isDisplayed(), isClickable(), waitForPageToLoad() method. It will buy some time before clicking on that element.

5. Use Hardcode for debugging purpose. Check that, is it failing because element is not visible.

6. Try to do getText() for that element. If the locator is working, it should return text for that element also if it's present.

7. Try to find out window size of that jenkin machine where your code is running. There are chances, that the browser size is different in your local to Jenkin machine.

You can use below code for that:

Dimension dimension = driver.manage().window().getSize();

LOGGER.info("dimension is " + dimension);

Later set the size.

driver.manage().window().setSize(new Dimension(1280, 768));

8. Use fullScreen, maxsize for browser.

driver.Manage().Window.Maximize();



Wednesday, 20 October 2021

October 20, 2021

[Fix] Selenium getText() is returning empty text in Jenkin while working fine in Local

 Today I encountered a weird issue where in my local. I am able to retrieve element text with the help of selenium getText() method while in jenkin machine, it was giving empty value for the element text.











It was happening because of the different view port in Jenkin. Mean, in Jenkin, that field was not visible because the size of the window was different there.

To fix this issue, rather then getting the text via getText() method, I used getAttribute method.

And I gave the property as "innerText".

So I replaced my getText() to getAttribute("innerText") and hurray, It returned the text now.

if this also doesn't work for you, try with below options:

1. getAttribute("innerHTML")

2. getAttribute("textContent")


This saved my lot of time because I was using scrollView method else to navigate that element first and then retrieve the text value.



Friday, 8 October 2021

October 08, 2021

[Karate] Read authentication token from config file

 When you start enhancing your karate framework, there comes a time when you want to read your auth token information from your config file, so that it can be set once, and reused in entire test suite run.

If you are in this stage or even if you are seeing how you can read your auth token from config file, then this post will help you.











So initially, I was having a token generation feature file which I was calling in my feature file with the help of callSingle method in background.

* def result = karate.callSingle('classpath:TokenGeneration.feature')

*def token = result.token

* def tokenId = result.tokenId

Once I get the otken and tokenId, I can use it in my feature file.

And header token = token

And header tokenId = tokenId

Later, I thought to put this token feature inside karate config file, so that these values will be set once, and we can reuse it again.

So, for this, we need to go to karate config file and at the bottom, before return config, call this token feature.

var result =  karate.callSingle('classpath:TokenGeneration.feature', config);

authInfo = {authToken:result.token, authTokenId: result.tokenId};

return config;


Now in your feature file, you can use it like in this manner:

*def token =authInfo.authToken

* def tokenId = authInfo.authTokenId