1

In my automated code, trying to match background color of a web-element 'Find the best card for me' text.

Console view :

enter image description here

To do it, I have to identify that web-element on page first, get the color, store in String as expected value.

Below code does the same:

WebElement slickDotButton2Presence = driver.findElement(homepageobjectsloc.slickDotButton2);
slickDotButton2Presence.click();
String findTheBestCarsForMeTextBackgroundColour = driver.findElement(homepageobjectsloc.secondBannerFindTheBestCardForMeText).getCssValue("background");

In website value is in hex, but Selenium method will return values in rgb So whatever value I got from above line of code need to convert into hex first and then have to compare with assert method.

Used below line of code:

try {
    String value = findTheBestCarsForMeTextBackgroundColour.trim();
    String[] rgbs = value.split("\\)")[0].split("\\(")[1].split(",");
    long r = Long.parseLong(rgbs[0]);
    long g = Long.parseLong(rgbs[1]);
    long b = Long.parseLong(rgbs[2]);
    String hex = String.format("#%02x%02x", r, g, b);
    System.out.println("=> The hex conversion is : " + hex);
    Assert.assertEquals("#fff", hex);
}

But when I execute it , getting below error:

=> The hex conversion is : #ffff
java.lang.AssertionError: expected [#ffff] but found [#fff]
    at org.testng.Assert.fail(Assert.java:94)
    at org.testng.Assert.failNotEquals(Assert.java:513)
    at org.testng.Assert.assertEqualsImpl(Assert.java:135)
    at org.testng.Assert.assertEquals(Assert.java:116)
    at org.testng.Assert.assertEquals(Assert.java:190)
    at org.testng.Assert.assertEquals(Assert.java:200)
    at tests.homepage.HomePageStepDefinitions.verify_that_Find_the_best_card_for_me_text_is_available_on_the_second_banner_in_hompage_then_click_on_it(HomePageStepDefinitions.java:795)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils$1.call(Utils.java:40)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:34)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
    at cucumber.runtime.Runtime.runStep(Runtime.java:300)
    at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
    at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
    at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

How to convert onto hex and make test pass?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Avinmita
  • 69
  • 1
  • 9

2 Answers2

2

Try selenium library

import org.openqa.selenium.support.Color;
String value = findTheBestCarsForMeTextBackgroundColour.trim();
String hex = Color.fromString(value).asHex();
System.out.println("=> The hex conversion is : " + hex);
Assert.assertEquals("#fff", hex);

You can refer selenium official documentation here

Below is the test case in selenium junit tests written for your case. Make sure the rgb String you are passing in Color.fromString("rgbString") should be in format the function expects.

  @Test
  public void rgbToHex() {
    String hex = "#01ff03";
    String rgb = "rgb(1, 255, 3)";
    assertThat(Color.fromString(rgb).asHex()).isEqualTo(hex);
  }
Amit Jain
  • 4,389
  • 2
  • 18
  • 21
  • Getting this error -------- java.lang.IllegalArgumentException: No enum constant org.openqa.selenium.support.Colors.RGB(255, 255, 255) NONE REPEAT SCROLL 0% 0% / AUTO PADDING-BOX BORDER-BOX – Avinmita Feb 19 '19 at 11:03
  • @Avinmita - Please check the new answer and also check official junit tests for this case.Thanks – Amit Jain Feb 19 '19 at 13:07
-1

From your code trials, presumably you are getting back:

  • r -> 255
  • g -> 255
  • b -> 255

Console view

As per the Console view you are seeing: #fff. @DanHerbert in this discussion mentions, it is the compressors that will intelligently convert #ffffff to #fff for optimizing page-loading speed as network latency, bandwidth and parsing time matters more than processing time in general.

To deal with these cases, @T.J.Crowder in this discussion suggests to look for a string starting with # followed by three pairs of matching hex digits, and replace them with just the short form before you consider for assertion as follows:

static String getHex(int r, int g, int b) {
    return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
}

Sample implementation:

  • Code Block:

    package demo;
    
    public class Background_White {
    
        public static void main(String[] args) {
    
            System.out.println(getHex(255, 255, 255)); // #fff
        }
    
        static String getHex(int r, int g, int b) {
            return String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
        }
    }
    
  • Console Output:

    #fff
    

In your usecase you can use it as:

try {
    String value = findTheBestCarsForMeTextBackgroundColour.trim();
    String[] rgbs = value.split("\\)")[0].split("\\(")[1].split(",");
    long r = Long.parseLong(rgbs[0]);
    long g = Long.parseLong(rgbs[1]);
    long b = Long.parseLong(rgbs[2]);
    String hex = String.format("#%02x%02x%02x", r, g, b).replaceAll("^#([a-fA-F])\\1([a-fA-F])\\2([a-fA-F])\\3$", "#$1$2$3");
    System.out.println("=> The hex conversion is : " + hex);
    Assert.assertEquals("#fff", hex);
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352