<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suryajit Bedage on Medium]]></title>
        <description><![CDATA[Stories by Suryajit Bedage on Medium]]></description>
        <link>https://medium.com/@suryajit7?source=rss-5937cbc34d8c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SH-UV8DqFezzFC4r1JFVYA@2x.jpeg</url>
            <title>Stories by Suryajit Bedage on Medium</title>
            <link>https://medium.com/@suryajit7?source=rss-5937cbc34d8c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 10:25:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryajit7/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Visual Assertions in UI Tests]]></title>
            <link>https://suryajit7.medium.com/visual-assertions-in-ui-tests-c17d8544acb1?source=rss-5937cbc34d8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c17d8544acb1</guid>
            <category><![CDATA[visual-validation]]></category>
            <category><![CDATA[screenshot-testing]]></category>
            <category><![CDATA[visual-testing]]></category>
            <category><![CDATA[visual-regression-testing]]></category>
            <dc:creator><![CDATA[Suryajit Bedage]]></dc:creator>
            <pubDate>Thu, 15 Sep 2022 21:42:09 GMT</pubDate>
            <atom:updated>2023-02-15T14:23:14.680Z</atom:updated>
            <content:encoded><![CDATA[<h4>Automation Framework Customization: Part-1 | Expertise: Chūnin</h4><p>In this article, we will look at how we can create our own custom Visual Assertion class to compare screenshots and optimize it further to visually validate the correctness and appearance of any given UI components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*ejYXRWQ3dIpDvJCKtVbK4w.png" /><figcaption><strong>Visual Validation of UI Components</strong></figcaption></figure><p>But first, let&#39;s understand why our UI tests fail to give us the confidence that we all are longing for. This is because typical UI tests either →</p><ul><li>Renders the component in a virtual DOM (i.e. unit/component tests)</li><li>Load the entire page including the component in a real browser (acceptance/e2e)</li></ul><p>And in both aforementioned cases, we end up checking whether the given component has rendered correctly in a DOM or located (loaded) in a browser; however, we still cannot verify if a given component is correctly visible on UI “<strong><em>as it should be</em></strong>” and NOT spilt over, distorted, overlapped or simply incorrect in any way.</p><p>So does this mean, no matter how good our UI tests are, no one in this deciduous<strong> </strong>world can guarantee that each and every component of a given UI can be verified by just running such automated UI tests?<br>And thus, we will always need the expertise of a mere earthling to use their human eyes to give us that, “<strong><em>everything looks good</em></strong>” confidence.</p><p>Well, the above thought process is simply bullshit!</p><p>Let&#39;s solve this issue by Visually Asserting Screenshots of WebElements in our UI tests. We will primarily use <a href="https://github.com/assertthat/selenium-shutterbug"><strong>Shutterbug</strong></a><strong> </strong>and <a href="https://github.com/assertj/assertj"><strong>AssertJ</strong></a> libraries to perform automated visual validations.</p><p><strong>Prerequisites:<br></strong>• <a href="https://github.com/suryajit7/my-blog-topics">UI Test Project</a> using Selenium WebDriver</p><p><strong>Step 1</strong>: Add the <a href="https://github.com/assertthat/selenium-shutterbug"><strong>Shutterbug</strong></a> and <a href="https://github.com/assertj/assertj"><strong>AssertJ</strong></a><strong> </strong>dependencies to your build file.</p><p>For Maven: update pom.xml</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;com.assertthat&lt;/groupId&gt;<br>    &lt;artifactId&gt;selenium-shutterbug&lt;/artifactId&gt;<br>    &lt;version&gt;1.6&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.assertj&lt;/groupId&gt;<br>    &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;<br>    &lt;version&gt;3.23.1&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p>For Gradle: update build.gradle</p><pre>implementation &#39;com.assertthat:selenium-shutterbug:1.6.0&#39;<br>implementation &#39;org.assertj:assertj-core:3.23.1&#39;</pre><p><strong>Step 2:</strong> Create an <strong>AssertSnapshot </strong>class</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/175f11c49ac5c83633e947de9eaad71b/href">https://medium.com/media/175f11c49ac5c83633e947de9eaad71b/href</a></iframe><blockquote>Note: You may want to move both methods <strong>takeWebElementScreenshot() </strong>and <strong>getFilePathForFile() </strong>to your Test Starter class like BaseTest which is extended by all other tests in the project.</blockquote><p><strong>Step 3:</strong> Add Visual Assertions to your UI Tests</p><p>Let’s create a test class which has two test methods.</p><ul><li>Test 1: The first test will take a screenshot of the Google Search component at runtime and compare it with an expected image which is correct so it will pass.</li><li>The second test, however, compares the runtime screenshot with an incorrect image to show the test failing.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84648645a20aff32e98b98c7afb51b22/href">https://medium.com/media/84648645a20aff32e98b98c7afb51b22/href</a></iframe><blockquote>Note: Before comparing a screenshot, you wil need to create an expected screenshot. You can do this by simply executing <strong>takeWebElementScreenshot() </strong>first without performing any Visual Assertion.</blockquote><p>Finally below are two expected images we will use in tests accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*6nfsc_-uF2cJ20ksZXsXFw.png" /><figcaption><strong>Correct Expected Image used in Test-1</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*dAMIXXGFwlYmnUH2h2ujYg.png" /><figcaption><strong>Incorrect Expected Image used in Test-2</strong></figcaption></figure><p><strong>Step 4: </strong>Now run your tests and verify the functionality.</p><p>As expected Test-1 will pass with the correct expected image and Test-2 will fail with the incorrect expected image. Below are the test-run results displayed in IDE.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*Vu6c3RpFUEF4wlWd5WWfzQ.png" /><figcaption><strong>Testrun Results in IDE</strong></figcaption></figure><p>Our failed Visual Assertion will also create a new Image with the differences highlighted in red as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*HRmtbbl-qi00KU55lPRwDg.png" /><figcaption><strong>Resultant Image Highlighting Differences in Red</strong></figcaption></figure><p><strong>Step 5:</strong> Configure Browser Size</p><p>Your visual assertions may still fail if the resolutions of both the expected and actual images differ. One way to resolve this is by setting the browser window to a default size just after instantiating Selenium WebDriver.</p><pre>driver.manage().window().setSize(new Dimension(1920, 1080));</pre><p><strong>Step 6: </strong>Calibrate Deviation Rate</p><p>Shutterbug library allows us to set the deviation rate which represents the percentage of image difference between the expected and actual images.</p><p>However, if you set this to 0.0, it will fail the test for even minuscule image differences. Therefore adjust this rate to a value which works as per your requirements. (like 0.1 or 0.001)</p><p><strong>Links:</strong></p><p>The above illustration is a tiny part of my personal project Sharingan; however, I have added the minimum required code to my Github project which is used in this article.</p><p><a href="https://github.com/suryajit7/my-blog-topics">GitHub - suryajit7/my-blog-topics: This is just a test project to showcase sample code for ideas discussed in my online blogs.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c17d8544acb1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS Cognito Authentication for API Testing with REST-assured]]></title>
            <link>https://suryajit7.medium.com/aws-cognito-authentication-for-api-testing-with-rest-assured-8c8edd57aba8?source=rss-5937cbc34d8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c8edd57aba8</guid>
            <category><![CDATA[restassured]]></category>
            <category><![CDATA[api-testing]]></category>
            <category><![CDATA[aws-cognito]]></category>
            <category><![CDATA[aws-api-gateway]]></category>
            <dc:creator><![CDATA[Suryajit Bedage]]></dc:creator>
            <pubDate>Sat, 28 May 2022 13:12:50 GMT</pubDate>
            <atom:updated>2022-09-16T06:48:29.654Z</atom:updated>
            <content:encoded><![CDATA[<h4>AWS in Test Automation | Expertise: Chūnin</h4><p>In this article, we will use AWS library to automatically authenticate a given user secured by AWS Cognito and then run automated API tests.</p><p>AWS Cognito Service provides Authentication, Authorization, and User Management (as a) Service for any application built on top of AWS infrastructure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uF6PhSnHRemVtrVEWVfU7Q.png" /><figcaption><strong>AWS Cognito + REST-assured</strong></figcaption></figure><p>Below is a quick intro to AWS Cognito.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FabTy-Yyo6lI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DabTy-Yyo6lI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FabTy-Yyo6lI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/3760d171f559c62d7cbbc3630b35065a/href">https://medium.com/media/3760d171f559c62d7cbbc3630b35065a/href</a></iframe><p><strong>Prerequisites:<br></strong>• App developed on top of AWS infra which uses AWS Cognito<br>• API Test Automation Framework</p><p><strong>Step 1: </strong>Get necessary details of your application hosted on AWS and the User used for authentication in automated API tests.</p><p>• Pool id<br>• App client id<br>• App client secret<br>• Region<br>• Username<br>• Password<br>• Access key ID<br>• Access key security</p><blockquote><strong>Note: </strong>To create a User Access Key for any given User<br> → Click on your username near the top right and select Security Credentials.<br> → Create a new Access Key (or use existing) present under the Access keys tab.</blockquote><p><strong>Step 2: </strong>Add <strong>aws-java-sdk </strong>dependency in your Maven pom.xml or Gradle build file.</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;<br>    &lt;artifactId&gt;aws-java-sdk&lt;/artifactId&gt;<br>    &lt;version&gt;1.12.228&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p>For Gradle:</p><pre>implementation &#39;com.amazonaws:aws-java-sdk:1.12.228&#39;</pre><p><strong>Step 3:</strong> Create an AuthenticationHelper class</p><p>This helper class is used to perform SRP based client-side mathematical calculations for any given AWS Application Pool ID.</p><pre>public class AuthenticationHelper<strong> </strong>{<br><br>    private static final BigInteger <em>bigIntegerWithGivenPower </em>= BigInteger.<em>valueOf</em>(2);<br>    private static final BigInteger <em>magnitudeBigInteger</em>;<br>    private static final int <em>EPHEMERAL_KEY_LENGTH </em>= 1024;<br>    private static final SecureRandom <em>SECURE_RANDOM</em>;<br><br>    private BigInteger randomBigInteger;<br>    private BigInteger bigInteger;<br>    private String poolName;<br><br>    // Seems to be 3072-bit Group from rfc5054<br>    private static final String <em>HEX_N </em>=<br>            &quot;FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1&quot;<br>                    + &quot;29024E088A67CC74020BBEA63B139B22514A08798E3404DD&quot;<br>                    + &quot;EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245&quot;<br>                    + &quot;E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED&quot;<br>                    + &quot;EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D&quot;<br>                    + &quot;C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F&quot;<br>                    + &quot;83655D23DCA3AD961C62F356208552BB9ED529077096966D&quot;<br>                    + &quot;670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B&quot;<br>                    + &quot;E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9&quot;<br>                    + &quot;DE2BCBF6955817183995497CEA956AE515D2261898FA0510&quot;<br>                    + &quot;15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64&quot;<br>                    + &quot;ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7&quot;<br>                    + &quot;ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B&quot;<br>                    + &quot;F12FFA06D98A0864D87602733EC86A64521F2B18177B200C&quot;<br>                    + &quot;BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31&quot;<br>                    + &quot;43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF&quot;;<br><br>    private static final BigInteger <em>newBigInteger </em>= new BigInteger(<em>HEX_N</em>, 16);<br><br>    public AuthenticationHelper(String userPoolName) {<br>        do {<br>            randomBigInteger = new BigInteger(<em>EPHEMERAL_KEY_LENGTH</em>, <em>SECURE_RANDOM</em>).mod(<em>newBigInteger</em>);<br>            bigInteger = <em>bigIntegerWithGivenPower</em>.modPow(randomBigInteger, <em>newBigInteger</em>);<br>        } while (bigInteger.mod(<em>newBigInteger</em>).equals(BigInteger.<em>ZERO</em>));<br><br>        if (userPoolName.contains(&quot;_&quot;)) {<br>            poolName = userPoolName.split(&quot;_&quot;, 2)[1];<br>        } else {<br>            poolName = userPoolName;<br>        }<br>    }<br><br><br>    public BigInteger getRandomBigInteger() {<br>        return bigInteger;<br>    }<br><br>    private static final ThreadLocal&lt;MessageDigest&gt; <em>THREAD_MESSAGE_DIGEST </em>=<br>            ThreadLocal.<em>withInitial</em>(() -&gt; {<br>                try {<br>                    return MessageDigest.<em>getInstance</em>(&quot;SHA-256&quot;);<br>                } catch (NoSuchAlgorithmException e) {<br>                    throw new CognitoInternalErrorException(&quot;Exception in authentication&quot;, e);<br>                }<br>            });<br><br>    static {<br>        try {<br>            <em>SECURE_RANDOM </em>= SecureRandom.<em>getInstance</em>(&quot;SHA1PRNG&quot;);<br><br>            MessageDigest messageDigest = <em>THREAD_MESSAGE_DIGEST</em>.get();<br>            messageDigest.reset();<br>            messageDigest.update(<em>newBigInteger</em>.toByteArray());<br>            byte[] digest = messageDigest.digest(<em>bigIntegerWithGivenPower</em>.toByteArray());<br>            <em>magnitudeBigInteger </em>= new BigInteger(1, digest);<br>        } catch (NoSuchAlgorithmException e) {<br>            throw new CognitoInternalErrorException(e.getMessage(), e);<br>        }<br>    }<br>}</pre><p><strong>Step 4:</strong> Create AccessTokenManager class</p><p>This class will be used to generate user credentials using Java SDK provided by AWS. The</p><p>I have highlighted the constants used in the below code snippet for which the values were identified in <strong>Step 1</strong> itself.</p><blockquote><strong>Note: </strong>It is advised to use these constants as environment variables fetchable during runtime. Something like below.<br> → <em>System.getenv(“</em><strong><em>APP_USERNAME</em></strong><em>”);</em></blockquote><pre><em>/**<br> * This class will generate Access Token to access the APIs for any given AWS App.<br> * It calls the InitiateAuth operation with User Credentials with AppUsername and SRP.<br> * We&#39;re using AmazonCognitoIdentity credentials object {AWSCognitoIdentityCredentials}<br> * to enable authentication of users through third-party identity providers.<br> */<br></em>@Slf4j<br>public class AccessTokenManager {<br><br>    <em>/**<br>     * This method uses AWS CognitoIdentityCredentials to authenticate given user.<br>     * Note: The given user should already be created within the AWS User Identity Pool with identity providers attached.<br>     */<br>    </em>public static AWSCognitoIdentityProvider getAmazonCognitoIdentityClient() {<br>        BasicAWSCredentials creds = new BasicAWSCredentials(<strong><em>ACCESS_KEY_ID</em></strong>, <strong><em>SECRET_ACCESS_KEY</em></strong>);<br>        return AWSCognitoIdentityProviderClientBuilder.<em>standard</em>()<br>           .withCredentials(new AWSStaticCredentialsProvider(creds))<br>           .withRegion(<strong><em>REGION</em></strong>)<br>           .build();<br>    }<br><br>    <em>/**<br>     * </em><strong><em>@return </em></strong><em>This method will generate unique AccessToken consumable by the Login Service to generate a Bearer Token.<br>     */<br>    </em>public static String getAccessToken() {<br><br>        AuthenticationResultType authenticationResult = null;<br>        AWSCognitoIdentityProvider cognitoClient = <em>getAmazonCognitoIdentityClient</em>();<br><br>        final Map&lt;String, String&gt; authParams = new HashMap&lt;&gt;();<br><br>        authParams.put(&quot;USERNAME&quot;, <strong><em>USERNAME</em></strong>);<br>        authParams.put(&quot;PASSWORD&quot;, <strong><em>PASSWORD</em></strong>);<br>        authParams.put(&quot;SRP_A&quot;, new AuthenticationHelper(<strong><em>POOL_ID</em></strong>).getRandomBigInteger().toString(16));<br><br>        final InitiateAuthRequest authRequest = new InitiateAuthRequest();<br><br>        authRequest.withClientId(<strong><em>CLIENT_ID</em></strong>)<br>                .withAuthParameters(authParams)<br>                .withAuthFlow(<em>USER_PASSWORD_AUTH</em>);<br><br>        InitiateAuthResult result = cognitoClient.initiateAuth(authRequest);<br>        authenticationResult = result.getAuthenticationResult();<br>        String accessToken = authenticationResult.getAccessToken();<br><br>        cognitoClient.shutdown();</pre><pre>        return accessToken;<br>    }<br>}</pre><blockquote><strong>Note: </strong>Do checkout the various get methods accessible by authenticationResult and use it as per your need. The returned accessToken is all we need to authenticate our API requests.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*2BicU3hfLauaEbSVJWuAag.png" /></figure><p><strong>Step 5: </strong>Create an API test using REST-assured java library</p><p>This test will use the accessToken generated in <strong>Step 4</strong>to authenticate the API requests made by our API tests.</p><pre>public class APITestDemo {<br><br>    private RequestSpecification requestSpec;<br>    private ResponseSpecification responseSpec;<br>    <br><em>    //Below constants are just used as an example. <br>    //Replace it with your application endpoints. </em><br>    private static final String <em>APP_ENDPOINT </em>= &quot;https://service-code.region-code.amazonaws.com&quot;;<br>    private static final String <em>LOGIN_ROUTE </em>= &quot;/login&quot;;<br><br>    @BeforeClass<br>    public void beforeClassSetup(){<br><br>        requestSpec = new RequestSpecBuilder()<br>                    .setBaseUri(<em>APP_ENDPOINT</em>)<br>                    .setContentType(<em>JSON</em>)<br>                    .log(<em>ALL</em>).build();<br><br>        responseSpec = new ResponseSpecBuilder()<br>                .log(<em>ALL</em>).build();<br>    }<br><br>    @Test<br>    public void validateAuthFlowForAPITestDemo(){<br>        <br>        <em>given</em>(requestSpec)<br>                .auth().oauth2(<strong>AccessTokenManager.<em>getAccessToken</em>()</strong>)<br>                .when()<br>                .get(<em>LOGIN_ROUTE</em>)<br>                .then()<br>                .spec(responseSpec)<br>                .extract().response();<br>    }<br>}</pre><p><strong>Step 6: </strong>Run the API test<strong> </strong>created in <strong>Step 5</strong></p><p>You should get a successful JSON log response (depending on your config) after running your test.</p><p>Now you can add more API tests as per your need using the above authentication method.</p><p><strong>Bonus: </strong>We can further refactor our code to remove boilerplate for AWS variables used in <strong>Step 4 </strong>within <strong>AccessTokenManager </strong>class. So the <strong><em>getAmazonCognitoIdentityClient()</em></strong> method can be rewritten as below.</p><pre>public static AWSCognitoIdentityProvider getAmazonCognitoIdentityClient() {<br>    return AWSCognitoIdentityProviderClientBuilder.<em>standard</em>()<br>            .build();<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c8edd57aba8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Your Dockerized Tests in Gitlab CI-CD Pipeline]]></title>
            <link>https://suryajit7.medium.com/run-your-dockerized-tests-in-gitlab-ci-cd-pipeline-a6dce75b7be3?source=rss-5937cbc34d8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6dce75b7be3</guid>
            <category><![CDATA[dockerized-tests]]></category>
            <category><![CDATA[ci-cd-tests]]></category>
            <category><![CDATA[gitlab-tests]]></category>
            <dc:creator><![CDATA[Suryajit Bedage]]></dc:creator>
            <pubDate>Mon, 25 Apr 2022 22:14:34 GMT</pubDate>
            <atom:updated>2022-12-01T19:26:58.142Z</atom:updated>
            <content:encoded><![CDATA[<h4>Test Dockerization, Run and Report: Part-2 | Expertise: Genin</h4><p>You may want to run your dockerized tests as it is on your <a href="https://docs.gitlab.com/ee/ci/pipelines/index.html">CI/CD pipeline</a> to verify any change introduced by your developers.</p><p>In this article, we will use the Docker Compose image along with the Docker-In-Docker service to run our dockerized tests in the Gitlab CI/CD pipeline.</p><p><strong>Note: </strong>As a thumb rule Docker in Docker image should be avoided for reasons briefly articulated in this <a href="http://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/">article</a>. This article merely shows how we can use it to our advantage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FG8TIUzEmrg54mvO7zXT3Q.png" /><figcaption><strong>Gitlab + Docker</strong></figcaption></figure><p>Below is a quick intro to CI/CD Pipelines.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FscEDHsr3APg%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DscEDHsr3APg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FscEDHsr3APg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1a71905fb42de04757bbf1a4facfcea/href">https://medium.com/media/c1a71905fb42de04757bbf1a4facfcea/href</a></iframe><p><strong>Prerequisites:<br></strong><a href="https://gitlab.com/">Gitlab Account</a><br><a href="https://gitlab.com/suryajit7/my-blog-topics">Dockerized Test Project</a></p><p>You may view <a href="https://suryajit7.medium.com/run-your-dockerized-tests-in-gitlab-ci-cd-pipeline-a6dce75b7be3"><strong>my previous article</strong></a> on how to dockerize a Test Automation project or simply visit the <a href="https://gitlab.com/suryajit7/my-blog-topics"><strong>Gitlab project</strong></a> used in this article.</p><p><strong>Step 1: </strong>Add a <a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/.gitlab-ci.yml"><strong>.gitlab-ci.yml</strong></a> file to your project</p><p>We will use this .gitlab-ci.yml file configure specific instructions for our GitLab CI/CD pipeline.</p><pre><em>#Use the official docker image.</em><br><strong>docker-test:</strong><em><br></em><strong><em>  </em>image: docker/compose:latest<br>  stage: test<br>  services:<br>    - docker:dind</strong><br><br>  <em>#Instructions to run tests<br></em><strong><em>  </em>script:<br>    - docker-compose up -d --build</strong><br><br>  <em>#Run this job in a branch where a Dockerfile exists<br></em><strong><em>  </em>rules:<br>    - if: $CI_COMMIT_BRANCH<br>      exists:<br>        - Dockerfile<br>        - docker-compose.yml</strong></pre><p><strong>Step 2:</strong> Add a <a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/script.sh"><strong>script.sh</strong></a> to copy test results from the running docker container</p><p>The below script will first try to find the test container-id and then issue a copy command.</p><pre><strong>apk add bash<br>var=$(docker ps -alq)<br>export CONTAINER_ID=&quot;$var&quot;<br>echo $CONTAINER_ID<br>docker cp $CONTAINER_ID:./app .<br>docker cp $CONTAINER_ID:./jar .</strong></pre><p><strong>Step 3:</strong> Modify your <strong>.gitlab-ci.yml</strong> file to run the earlier added script.</p><p>Add the below lines under <strong>script:</strong></p><pre><em>#Instructions to run tests<br></em><strong>script:<br>  - docker-compose up -d --build<br>  - docker wait test-run<br>  - chmod +x ./script.sh<br>  - ./script.sh</strong></pre><p><strong>Step 4: </strong>Configure<strong> </strong>Junit report to display Test Results in Gitlab CI/CD pipeline</p><p>Add the below steps in <strong>.gitlab-ci.yml. </strong>Modify the report path wherever your JUnit reports are available.</p><pre><strong>artifacts:<br>  reports:<br>    junit:<br>      - jar/test-output/junitreports/TEST-*.xml</strong></pre><p><strong>Step 5: </strong>Push the code with <strong>.gitlab-ci.yml</strong></p><p>It should run your docker container which contains your tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*tPW-K18QhkAN5lX5KAu5TQ.png" /><figcaption><strong>Dockerized Tests Status: Running</strong></figcaption></figure><p>Once completed, you can navigate to the <a href="https://gitlab.com/suryajit7/my-blog-topics/-/pipelines/518134820/test_report"><strong>Tests</strong></a><strong> </strong>sections to view the test results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FnKVv6-hYWzsf6TgvwSc3g.png" /><figcaption><strong>Dockerized Tests: Test Results</strong></figcaption></figure><p><strong>Bonus: </strong>Gitlab has very limited features to run tests and then fail the pipeline following a test-run failure.</p><p>However, we can write our own custom logic to achieve this. In the below example I have used the TestNG test runner to run my tests. TestNG generates various files including a <strong>testng-failed.xml </strong>in case of test failures<strong>. </strong>We can check the presence of this file to fail the pipeline.</p><pre><strong>variables:<br>  TESTNG_FAILED_XML: jar/test-output/testng-failed.xml<br>script:<br>  - docker-compose up -d --build<br>  - docker wait test-run<br>  - chmod +x ./script.sh<br>  - ./script.sh<br>  - |<br>    if [[ -f &quot;$TESTNG_FAILED_XML&quot; ]]; then<br>      echo &quot;Test failures observed as TestNG-Failed file exists.&quot;<br>      exit 1<br>    else<br>      echo &quot;All tests passed Successfully.&quot;<br>    fi</strong></pre><p><strong>Links:</strong></p><p>Link to my GitLab project used in this example.<br>The project contains a lightweight framework capable of running both UI and API tests.</p><p><a href="https://gitlab.com/suryajit7/my-blog-topics">Suryajit / my-blog-topics</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6dce75b7be3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dockerize Your Selenium Grid Setup]]></title>
            <link>https://suryajit7.medium.com/dockerize-your-selenium-grid-setup-23807b2bccc9?source=rss-5937cbc34d8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/23807b2bccc9</guid>
            <category><![CDATA[selenium-grid]]></category>
            <category><![CDATA[dockerize]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[selenium-grid-4]]></category>
            <dc:creator><![CDATA[Suryajit Bedage]]></dc:creator>
            <pubDate>Thu, 14 Apr 2022 19:59:27 GMT</pubDate>
            <atom:updated>2022-12-01T19:26:21.368Z</atom:updated>
            <content:encoded><![CDATA[<h4>Test Dockerization, Run and Report: <strong>Part-1</strong> | Expertise: Genin</h4><p>If your tests pass locally and fail in higher environments, then the best way to ensure consistent behaviour for your tests with greater efficiency is to dockerize them.</p><p>In this article, we will first set up a Selenium Grid and then dockerize it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*bm2gQVWn-gMCbv5LsBci1A.png" /><figcaption><strong>Dockerized Selenium Grid</strong></figcaption></figure><p>Below is a quick intro to Docker containers.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FGjnup-PuquQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DGjnup-PuquQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FGjnup-PuquQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube-nocookie" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/feffafa882151dcf90b89ccc962b82fc/href">https://medium.com/media/feffafa882151dcf90b89ccc962b82fc/href</a></iframe><p><strong>Prerequisite: <br></strong>Java-8 <br>Docker Desktop client<br>UI Test Project</p><p>Let&#39;s assume your Test Project uses TestNG to run its selenium scripts.</p><p><strong>Step 1</strong>: Modify your BaseTest or Test starter class to initialize RemoteWebDriver.</p><p>Replace or add the below code to your BaseTest or wherever your Browser Driver initialization happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*eKKSqSAfOL4_aNXHtlBPJA.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/src/test/java/functional/BaseTest.java"><strong>RemoteWebDriver Initialization</strong></a></figcaption></figure><p><strong>Step 2: </strong>Add Maven Plugins to your <strong>pom.xml</strong> to create executable jars.</p><p>We will need to add the below plugins in order to generate an executable jar for our Test Project.</p><pre>maven-compiler-plugin<br>maven-dependency-plugin<br>maven-jar-plugin</pre><p>However, by default maven packages only the source code under the <a href="https://gitlab.com/suryajit7/my-blog-topics/-/tree/main/src"><strong>main</strong></a> folder, and in case your Test Project has testing-related code under the main and test folders then we need to explicitly mention this in the pom.xml file.</p><p>[Your Test Project may follow the <a href="https://www.browserstack.com/guide/page-object-model-in-selenium#:~:text=Page%20Object%20Model%2C%20also%20known,application%20as%20a%20class%20file."><strong>Page Object Model</strong></a> design pattern where your Page classes may reside in the <strong>main </strong>and Test classes under the <strong>test </strong>folder.]</p><p>Refer to the execution goal at line 51 in the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*M_6bhRz8BQIx8MKL8Edzjw.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/pom.xml"><strong>pom.xml</strong></a></figcaption></figure><p><strong>Step 3: </strong>Add a <strong>healthcheck.sh </strong>script</p><p>The healthcheck script is needed to check the Grid availability to receive test requests before running our tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*qH-DG6xno2Ipm5Q5_6-mkw.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/healthcheck.sh"><strong>healthcheck.sh</strong></a></figcaption></figure><p><strong>Step 4: </strong>Add a <strong>run.sh </strong>script</p><p>This script will execute the jar files containing tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*K2V_95frcOiXIXB8LTAJLg.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/run.sh"><strong>run.sh</strong></a></figcaption></figure><p><strong>Step 5: </strong>Create a <strong>docker-compose.yml </strong>file</p><p>The below docker-compose file will let you run Selenium Grid with all its components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*o00I5A-aYbTsrTp1SoRvAg.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/docker-compose.yml"><strong>docker-compose</strong></a></figcaption></figure><p><strong>Step 6: </strong>Create a <a href="https://docs.docker.com/engine/reference/builder/"><strong>Dockerfile</strong></a> to instruct the creation of your Docker Image</p><p>We will use multiple base images in multi-stage builds to create the final image. The first stage (named <strong><em>package</em></strong>) will generate the executable jars and the second stage (named <strong><em>testrun</em></strong>) will run these executable jars.</p><p>Do note the different base images being used in each stage as required.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*KyKMmx3br091tf52715XWQ.png" /><figcaption><a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/Dockerfile"><strong>Dockerfile</strong></a></figcaption></figure><p><strong>Note:</strong> The above Dockerfile contains many RUN and shell commands listed separately for ease of understanding; however, we can combine them or run them together.</p><p>A smarter approach would be to use a Dockerfile Linter to ensure that the Dockerfile is written with the best practices. e.g. <a href="https://hadolint.github.io/hadolint/"><strong>Hadolint</strong></a></p><p><strong>Step 7: </strong>Finally, run your docker-compose</p><p>Open Powershell in your project root folder and run the below command.</p><p><strong><em>docker-compose up --build</em></strong></p><p><strong>Bonus: </strong>One can also dockerize their whole Automation Framework which may include API tests too. Just modify the <a href="https://gitlab.com/suryajit7/my-blog-topics/-/blob/main/testng.xml"><strong>testng.xml</strong></a><strong> </strong>accordingly to include the required tests.</p><p><strong>Links:<br></strong>Link to my GitLab project used in this example:<br>The project contains a lightweight framework capable of running both UI and API tests.</p><p><a href="https://gitlab.com/suryajit7/my-blog-topics">Suryajit / my-blog-topics</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23807b2bccc9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>