Interview Question

I have a textbox, button and a lable.

The requirement is, If I enter any text color (ex: green) and click the button. Then the lable shall fill the color with green.

Depending upon the color entering in the textbox, the lable shall fill with that respective color.

Can anybody let me know the Selenium WebDriver code in Java.

Add Comment
  • 3 Answer(s)

    String str = “green”;
    driver.findElement(By.id(“textbox”)).sendkeys(str);
    driver.findElement(By.id(“button”)).click();
    String strx =  driver.findElement(By.id(“lable”)).getCSSValue(“background-color”);
    if (str.equals(strx)){
    // test case passed
    }

    Supporter Answered on July 21, 2015.
    Add Comment

    package apache.poi.datadriven.anonymous;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class colorChangeOnClick {

    public void Test(){
    String str = “Green”;
    WebDriver driver = new FirefoxDriver();
    driver.findElement(By.id(“TextBox”)).sendKeys(str);
    driver.findElement(By.id(“button”)).click();

    switch(str){

    case “Green”:
    case “green”:
    ((JavascriptExecutor) driver).executeScript(“document.getElementById(‘label’).style.backgroundColor = ‘green’;”);
    break;

    case “Blue”:
    case “blue”:
    ((JavascriptExecutor) driver).executeScript(“document.getElementById(‘label’).style.backgroundColor = ‘blue’;”);
    break;

    default:
    break;
    }

    }
    }

    Train Answered on July 24, 2015.
    Add Comment

    Dear Suhabe, Amit

    Gr8 and Thanks for your response.

    Without hard coding the color in the program can we achieve the above requirement?

    Default Answered on July 24, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.