Automation Using Selenium Webdriver

Thursday, 20 October 2016

Verify Image Presence in Web Page using Selenium WebDriver


JavaScript executor is one of themost useful features of Selenium WebDriver. It allows Selenium user to perform many complex scenarioseasily. This post covers one such important scenario.Selenium verify image present example using WebDriver. We have used JavaScript executor in WebDriver Java code hereto verify if image is present in a web page.


@Test
public void CheckImage() throws Exception {
driver.get(baseUrl);
WebElement ImageFile = driver.findElement(By.xpath("//img[contains(@id,'Test Image')]"));
        
        Boolean ImagePresent = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", ImageFile);
        if (!ImagePresent)
        {
             System.out.println("Image not displayed.");
        }
        else
        {
            System.out.println("Image displayed.");
        }
}
Run your test. It should identify if image is displayed in web page and will print appropriate result in console

Page Object Model(SingIn-Page)Exp

By Using Page Object Model SignInPage  Store Locaters and Test  Best Example for (Singin-Page)

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

/**
 * Class which models the view of Sign-In page
 */
public class SignInPage {
 
    @FindBy(id="username")
    private usernameInput;

    @FindBy(id="password")
    private passwordInput;

    @FindBy(id="signin")
    private signInButton;

    private WebDriver driver;

    public SignInPage(WebDriver driver) {
        this.driver = driver;
    }
 
    /**
     * Method to perform login
     */
    public HomePage performLogin(String username, String password) {
        usernameInput.sendKeys(username);
        passwordInput.sendKeys(password);
        signInButton.click();
        wait.ForPageToLoad();
        return PageFactory.initElements(driver, HomePage.class);
    }
}


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
/**
 * Class which models the view of home page
 */
public class HomePage {
    @FindBy(id="logout")
    private logoutLink;

    private WebDriver driver;

    public HomePage(WebDriver driver) {
        this.driver = driver;
    }
 
    /**
     * Method to log out
     */
    public SignInPage logout() {
        logoutLink.click();
        wait.ForPageToLoad();
        return PageFactory.initElements(driver, SignInPage.class);
    }
}

/**
 * Login test class
 */
public class LoginTest {
    public void testLogin() {
        SignInPage signInPage = new SignInPage(driver);
        HomePage homePage = signInPage.login(username, password);
        signInPage = homePage.logout();
    }
}

Challenges faced while working on Selenium

Challenges faced while working on Selenium

There are many challenges we face while working on selenium. Some of the challenges are:

Challenges faced that are as follows:
·         Frequently changing UI. It always need to make changes in code most of the time.
·         Stuck somewhere while running automation scripts in chrome browser getting error that element is not visible, element not found.
·         New kind of element like ck-editor, bootstrap calendar and dynamic web tables. But get the solution always.
·         Reused of test scripts.
·         To be continued

1. configuration challenges on different machines
2. selenium's compatibility with browser versions like with FF20 is not supported with some web-driverversions.
3. handling IE/chrome etc.
4. popup handling
5. Ajax handling
6. security certificate handling
7. iframes
8. date pick from calender.
9. identifying locators
10. Upload and download file
11. Maintain the scripts to work properly irrespective of new build
12. Diff to identify script has failed may be unable to locate the element
or config issue(Browser updated) if your framework is not able to catch issue.
13. If you are using xpath as a locator. writing xpath without using dynamic changing data.
i.e., xpath generated dynamically.