QA & Automation – TestAutomasi Blog https://testautomasi.com/blog A blog On Test Automation Case Studies & Automation tools Sun, 02 Jul 2023 22:33:29 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Getting Started With Locust: A Python Based Performance Testing Tool https://testautomasi.com/blog/2021/06/13/getting-started-with-locust-a-python-based-performance-testing-tool/ Sun, 13 Jun 2021 19:54:02 +0000 https://testautomasi.com/blog/?p=173 Last year in one of the consulting assignments, I was asked to perform performance testing on an application using locust, client wanted to integrate load testing within backend code which was written in python.

Initially, I suggested JMeter but the client said a valid point that they want something that can easily be maintained by developers later on without much training.

I understood their point and started looking for python based load testing tool and within seconds I stumbled upon “Locust”. While Usage of this tool still quite low compare to other popular tools in this domain such as Jmeter, Loadrunner and Gatling but in terms of features, it is just as good as others.

Of course, being new and open source tool, it does have limitation but good part it, since it is pure python programming based tool, whatever is missing can easily be achieved through custom code and python third party libraries.

So in this locust series, We will discuss various ways to perform different types of performance testing and some common solutions to problems that I faced during my assignment.

Let’s start with a quick HTTP request test using the below steps…

  1. Installation

    Installation is pretty easy, you just need to run one single command, please note latest locust version required python 3.6 version or above.
pip3 install locust

2. Writing a quick load test of testautomasi home page in a python file:

from locust import HttpUser, task, between

class QuickLoadTest(HttpUser):
    wait_time = between(1, 10)

    @task
    def test(self):
        self.client.get("/")

Explanation of code: A class QuickLoadTest is created which extends the HtppUser locust class which has all the methods related to HTTP request testing as it is built on top of the python-requests module.

wait time is kind of a uniform random timer if you compare it with JMeter, which pauses threads for a random time interval which is between 1 to 10 in this case.
task annotation is like an HTTP request sample in Jmeter, under which you can write code to test one request or application flow with custom logic. Here, we are hitting the homepage(the base URL will be given during runtime) with the “get” method type.

3. Running test

Running this test is very straightforward, save the step 2 code as python file then open terminal/command prompt and run below command:

 locust -f ".\locationoffile.py"

After that, browse to locust web interface for providing base URL and thread count, by default at http://localhost:8089

Thread count is equivalent to the number of users in the locust, the spawn rate is equivalent to the ramp-up period in JMeter where 10 means, 10 threads will become active in 1 second till the total thread count reaches 100.

Host means the base URL/application URL, in this case, we are using testautomasi.com for demo purposes.

4. Viewing Results/Metrics:

Once you start tests, results can be viewed in real-time on the locust web interface in form of statistics, charts, failures, exceptions, and CSV data as shown below:

If you notice the above image closely, in locust, you can change thread count during runtime as well using the edit link just below status section, which isn’t possible in many other performance testing tools.

CSV report also produces data like summary report listener in Jmeter, you can download and view results metrics or send it to higher management/ Developers to take actions.

So That’s how you can use locust to perform your performance testing tasks, quite easy right?, Let’s discuss some more cool stuff about locust in upcoming articles, please leave your feedback about locust in the comment section.

]]>
Top 20 Best Practices For Writing Better Automation Test Code https://testautomasi.com/blog/2021/06/06/top-20-best-practices-for-writing-better-automation-test-code/ Sun, 06 Jun 2021 12:03:06 +0000 https://testautomasi.com/blog/?p=165 Programming Language Wise:

  • Follow Programming Language Guidelines( method name-getPayload(), class name- APIEndpoints, package name-com.testingsumo.backend.tests, variable name-authToken)
  • Follow Oops Concepts Wherever Possible- Abstraction(base classes), Inheritance(multiple implementations of same things/multiple inheritances), Polymorphism(many forms with something different), Data Hiding(hide unnecessary/sensitive info), Encapsulation(Bind small entities into a single large entity)
  • Reduce code duplication (think before writing new code, can I use/make changes in existing code?)
  • Increase code reusability 
  • Make your code generic wherever possible
  • Leave no hardcoded data in source code
  • Keep your static data outside the source code
  • Keep your dynamic data dynamic in test code (fetch it from DB queries or scripts)
  • Test your code properly, use IDE options such as call hierarchy or show usage to test your changes end 2 end

Framework and Debugging Wise

  • Use Extensive logging- everything which is part of source code should be analyzed from logs without looking at the source code
  • Generate and save failure proofs outside the src code- videos/data/screenshots/logs
  • Focus on making your code scalable and faster without compromising the code quality 
  • Your code should be platform and system  independent
  • Use as many assertions as possible focus on automated testing rather than automation
  • Leave no hardcoded data in source code
  • Always think for the future, separate out tech dependencies so that migration to new tech is easy in case it is needed
  • Keep your tests independent for better results in multithreading unless they are related (for example publisher-subscriber related tests)
  • Use Proper Documentation
  • Create code that can be easily read and modified by others

If you want to understand these best practices in a detailed manner with more depth and examples, please view the below video tutorials here-


If you have any best practices to suggest or have any feedback for us, please comment below.

]]>
How to run selenium 4 grid tests on Docker containers https://testautomasi.com/blog/2021/03/28/how-to-run-selenium-tests-on-docker-containers/ https://testautomasi.com/blog/2021/03/28/how-to-run-selenium-tests-on-docker-containers/#comments Sun, 28 Mar 2021 06:25:20 +0000 https://testautomasi.com/blog/?p=156 By using Docker containers for running chrome/browser drivers and selenium grid, you can run your selenium tests without the need for any dependency such as chrome, chrome driver or webdrivermanager locally.

When you use a remote selenium grid, then it also makes your local debugging easy, you can point to remote URL and see why your tests are failing on remote without the need to access Linux server and running debugging commands on a remote machine, below is a step by step guidance on how you can set up docker containers to run selenium tests-

-update Linux system
sudo apt-get update

-install docker
sudo apt install docker.io

-check docker version
sudo docker -v

-create compose.yml for selenium grid and chrome containers

To execute this docker-compose yml file use docker-compose -f docker-compose-v3.yml up
Add the -d flag at the end for detached execution
To stop the execution, hit Ctrl+C, and then docker-compose -f docker-compose-v3.yml down

version: “3”
services:
chrome:
image: selenium/node-chrome:4.8.3-20230328
shm_size: 2gb
depends_on:
– selenium-hub
environment:
– SE_EVENT_BUS_HOST=selenium-hub
– SE_EVENT_BUS_PUBLISH_PORT=4442
– SE_EVENT_BUS_SUBSCRIBE_PORT=4443

selenium-hub:
image: selenium/hub:4.8.3-20230328
container_name: selenium-hub
ports:
– “4442:4442”
– “4443:4443”
– “4444:4444”

yml reference- https://github.com/SeleniumHQ/docker-selenium/blob/trunk/docker-compose-v3.yml

-run containers using compose command (-d for detach mode)

-install compose and set permission

sudo apt install docker-compose

sudo docker-compose up

-see running containers info-

sudo docker ps

-see logs of the particular container

sudo docker logs containerid

-see performance stats of containers

sudo docker stats

-check grid is running

http://yourip:4444

run selenium tests using remote web driver URL of selenium hub
in your tests


@Test
	public void test() {
		WebDriver driver = null;

		ChromeOptions options = new ChromeOptions();
		options.addArguments("start-maximized"); // open Browser in maximized mode
		options.addArguments("disable-infobars"); // disabling infobars
		options.addArguments("--disable-extensions"); // disabling extensions
		options.addArguments("--disable-gpu"); // applicable to windows os only
		options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
		options.addArguments("--no-sandbox"); // Bypass OS security model
		options.addArguments("window-size=1200,1100");// set display size of window
		try {
			driver = new RemoteWebDriver(new URL("http://ip:4444/wd/hub"), options);
			driver.get("https://testautomasi.com");
			Thread.sleep(20000);
			assertEquals(driver.getTitle(), "Home - Welcome to automasi solutions private limited");
		} catch (MalformedURLException | InterruptedException e) {
			e.printStackTrace();
		}

		driver.quit();

	}


Have any feedback? Please don’t hesitate to leave it in the comments section.

]]>
https://testautomasi.com/blog/2021/03/28/how-to-run-selenium-tests-on-docker-containers/feed/ 1
How to create a compressed tar file with the relative file path in python https://testautomasi.com/blog/2021/03/06/how-to-create-a-compressed-tar-file-with-the-relative-file-path-in-python/ Sat, 06 Mar 2021 14:16:48 +0000 https://testautomasi.com/blog/?p=149 Whether you are working on file-heavy applications or on logs heavy applications, compressing files to save storage is very useful for efficient files management as well as disk storage management.

Recently I worked on a project which required compressing a set of files and folders and below are some useful things I noticed while creating a compressed tar file in python:

Create a simple tar file from a given path-

import tarfile

os.chdir(tar_location)
with tarfile.open(tar_location + '.tar', mode='w:tar') as browser_tar:
    browser_tar.add(tar_location)

so here, first, we move to the input directory after that, by using tarfile we add all the content of a folder in the tar file, simple enough right?

But there was a problem in this method, it creates a tar with absolute path i.e when you untar file then folder structure starts from input location, something like c://users/chandan/tarfilelocation.

Ideally when you want to send this tar as email content (like in the case of build logs) or send it to an application to start the processing of files(application-specific), then after creating a tar file, the structure of the folder should start from the relative folder location i.e. ./tarfilelocation in this case.

To see the whole path of your tar during debugging you can make use of getmembers() method which gives the info about the whole tar file path is shown below:

browser_tar.getmembers()

Now, to solve this problem tarfile module provides an extra argument called “arcname” and using it you can easily tar file with relative path without worrying about user directories as shown below:

browser_tar.add(tar_location, arcname=".")

and not only that, by using arcname, you can also provide a specific name too which could be very useful where you are playing with multiple tar files and you need to append something like a unique timestamp to create tar files with a unique name as shown below:

browser_tar.add(tar_location, arcname=unique_tar_name)


In this way, you can easily create compressed tar files and efficiently manage your disk space and set of files, do you have something to add then do leave your comment in the comments section.

]]>
Python Pytest Cheatsheet https://testautomasi.com/blog/2021/02/14/python-pytest-cheatsheet/ Sun, 14 Feb 2021 13:42:50 +0000 https://testautomasi.com/blog/?p=145 #installation pip install pytest #using in py files import pytest #run all tests with pytest pytest tests/ --where tests is a folder contains all test classes with starts or ends with test in name #grouping in tests --mark it in tests @pytest.mark.<markername> --then run using -m flag pytest tests/ -m "sanitytests" # calling pytests through python python -m pytest -q test_sample.py #run tests with specific keyword in test method name pytest tests/ -k "metric" #stop test run on first failure or max failure pytest tests/ -x pytest tests/ --maxfail=2 #retry failures pip install pytest-rerunfailures pytest tests/ --reruns 5 #disable test @pytest.mark.skip(reason="no way of currently testing this") #multithreading pip install pytest-xdist pytest tests/ -n 3 #ordering @pytest.mark.run(order=17) #using cmd parameters in pytest --supply param from cmd pytest tests\ --env qa --read from cmd in conftest file def pytest_addoption(parser): parser.addoption("--env", action="store", default="environment value is not provided") def pytest_sessionstart(session): env_param = session.config.getoption("--env") --set and get param using os.getenv os.environ['env'] = env_param os.getenv['env'] #fixtures/beforetest --create fixture in conftest @pytest.fixture def input_value(): input = 10 return input --use it in tests def test_divisible_by_3(input_value): assert input_value % 3 == 0 # data parameterization @pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)]) def test_multiplication_11(num, output): assert 11*num == output #reporting in pytest --pytest default html --pytest-html-reporter --junit pytest tests --junitxml="result.xml" --allure pytest tests --alluredir=/allure allure serve allure

]]>
Top 5 programming languages to learn as sdet in 2021 https://testautomasi.com/blog/2021/01/02/top-5-programming-languages-to-learn-as-sdet-in-2021/ Sat, 02 Jan 2021 14:30:16 +0000 https://testautomasi.com/blog/?p=140

1. Java– Open doors for selenium, appium,winapp, restassurred, karate, katalon cucumber, testNG, and serenity. Easy to learn, a lot of community support and wrappers are available, complex problems are easily available on Google/StackOverflow, jobs are easily found especially in India subcontinent and southeast Asia.

2. Python– Open doors for selenium, appium,winapp, requests, robot, pytest,behave and tkinter. Python is also very helpful for scripting, data analysis, and infrastructure automation tasks. Easier and faster to learn, learning resources are easily available, jobs are less compared to java but competition is also less compared to java.

3. JavaScript: Open doors for you in multiple testing tools such as cypress, Nightwatch, puppeteer,webdriverio, playwright, jest, supertest and postman. Slightly difficult to learn compared to other, learning resources are easily available, complex problem solution could be difficult to find. Companies in Europe and US prefer this over other languages.

4. C#– Open doors for you in almost all the things as java and very useful in desktop utility development. Resources are available on ms websites. Jobs-wise this language is quite popular in Australia and New Zealand.

5. Groovy: Open doors for JMeter, Gradle, SoapUI, katalon, Jira/Bitbucket, Jenkins. Very similar to java and easier to learn if you know java, very useful in Jira workflow automation, Jenkins scripted pipelines, and JMeter scripts, jobs are easier to find for performance tester with groovy knowledge.

]]>
Javascript Tutorials For Beginners https://testautomasi.com/blog/2020/12/26/javascript-tutorials-for-beginners/ Sat, 26 Dec 2020 07:44:33 +0000 https://testautomasi.com/blog/?p=132 In this video, you will learn JavaScript In 30 Minutes where IDE setup, Variables, Logics, Loops, List, Debugging, Functions & Json will be discussed.

]]>
How to make an impact on build quality as a sdet/automation engineer https://testautomasi.com/blog/2020/12/12/how-to-make-an-impact-on-build-quality-as-a-sdet-automation-engineer/ Sat, 12 Dec 2020 14:44:40 +0000 https://testautomasi.com/blog/?p=129 As a sdet/automation engineer if you are working with QAs/Test engineer after the release is deployed in QA then it’s already too late for you to make an impact in build quality, you need to work with developers and help them automate their unit testing and integration testing cycle by running automation suite or by providing them utilities/test scripts which can help them to deliver better quality builds to QA. Spend time with your developers to accelerate and enhance their testing activities and you would automatically see an increase in build quality and a decrease in build release timelines.

]]>
Programming Interview Questions For Testers(Experience Level 0-3 Years) Set 1 https://testautomasi.com/blog/2020/07/18/programming-interview-questions-for-testersexperience-level-0-3-set-1/ Sat, 18 Jul 2020 16:06:46 +0000 https://testautomasi.com/blog/?p=104 Write a program of sorting array without using a collection.

Write a  Program to reverse a string without using String inbuilt function.

Write a program to find the Factorial of a number using recursion.

Write a  Program to find whether a number is prime or not.

 Write a  Program to find whether a string or number is palindrome or not.

Write a Program for the Fibonacci series.

Write a  program for the Armstrong Number.

Write a  Program to find the duplicate characters in a string.

Write a  Program to Find the second-highest number in an array.

Write a  Program to Find the occurrence of Character in String.

Write a Program to Find the pairs of two-element of the array is 10.[8,2,7,6,3.5,4,2]

Write a Program to Find Duplicate String from an array

Write a Program to Find the second maximum value of a three-digit number.

Write a Program to push all zeros in last places- int [ ] Array={2,5,0,4,2,0,0,7,1,9,4}.

Write a program to find the missing number in given Array of sequential series, series length could be large too, Example-int [ ] array={1,2,4,5};

How to convert LinkedList to ArrayList?

How to convert HashSet to ArrayList(List) ?

How to sort ArrayList in Descending order?

Write a java program to implement Insertion sort?

Write a java program to implement a selection sort?

Write a java program to implement Bubble sort?

]]>
Selenium Interview Questions Set 1 https://testautomasi.com/blog/2020/07/02/selenium-interview-questions-set-1/ Thu, 02 Jul 2020 13:51:09 +0000 https://testautomasi.com/blog/?p=97
  • What is the difference between the close() and quit() method?

  • What is the difference between the get() and navigate() method?

  • What is the difference between the parentFrame() and defaultContent() method?

  • What is the difference between the getWindowHandle() and getWindowHandles()? What are the return types of these?

  • What is the difference between the W3C Protocol and JSON Wire Protocol?-Chandan

  • Explain Selenium HTTP API calls? How would you test selenium API using postman?-Chandan

  • Explain this line-
    webdriver driver = new chromedriver()
    -Chandan

  • What is the super interface of webdriver in Selenium Webdriver? Explain it in detail-Chandan

  • What is RemoteWebDriver? What is the purpose of using it over normal Webdriver?

  • What is the difference between WebDriver findElement and WebElement findElements? Can we use findelements to interact with single elements too? If no then Why not?

  • What is the difference between Selenium webDriver click() and Javascript click()? ‌ ‌Which one is better in terms of performance?

  • How to handle tabs, frames, and windows in Selenium?, How would you switch between tabs and windows efficiently?

  • How to handle alerts, modal and window pops in Selenium?

  • How to perform mouse hover and content drag and drop in Selenium?

  • How to drag and drop content from one frame to another where frames have no relation between each other ‌(By Chandan)

  • How to select/deselect checkboxes and radio buttons in Selenium?

  • What are the different waits provided by Selenium WebDriver?Tell us the use cases of each wait type a situation where each waittype would be more useful compared to other‌ ‌(By Chandan)

  • List different Exceptions you faced in Selenium WebDriver?‌ ‌Give an example of each?

  • What is StaleElementException? How will you fix this exception in your script?

  • In which condition sendKeys() can be used to upload files? If sendKeys() does not work then what are other alternatives?

  • How to select a date from the calendar using Selenium? Write a sample code for this.

  • How will you handle dynamic WebTable? Write a sample code for this.

    ‌ ‌
  • How will you scroll left, right, top, and down using Selenium? Write a sample code.

  • What is the difference between findElement() and @FindBy?

  • What is initElements Method? Why are we using this? What happens if we don’t use this?

  • How to get a typed text from a textbox?

  • How to fetch CSS properties such as font color, font size and background color using Selenium?-Chandan

  • How to fetch coordinates of element in Selenium? -Chandan

  • How to take screenshot of particular element in Selenium.-Chandan

  • How to resize browser window based on custom size provided by the user during runtime-Chandan


    Note: We have not provided answers as we believe you will learn more and remember more when you look for answers yourself and you will also improve your Research and Development Skills while doing that.
  • ]]>