hamidmousavi
hamidmousavi
Reputation Top 5%
hamid mousavi
3 Snippets  (174th place)
Published
1 Channel
Created
1 Channel
Following
Feb 20, 2017
Last Visit
Feb 3, 2017
Registered
70 points  (319th place)
Reputation
Junior Code Generator
Junior Publisher
Junior Trend Maker
Junior Autobiographer
Senior Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by hamidmousavi

public by hamidmousavi created Feb 18, 2017  2001  2  4  1

universal wrapper to make sure an element is visible

 public static boolean isElementPresentAndDisplayed(RemoteWebDriver driver, final WebElement element) {
        try {
            return element.isDisplayed();
        } catch (NoSuchElementException e) {
            return false;
        }
    }

....

  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(timeoutSeconds, TimeUnit.SECONDS)
                    .pollingEvery(1, TimeUnit.SECONDS)
                    .ignoring(NoSuchElementException.class)
                    .ignoring(StaleElementReferenceException.class)
                    .ignoring(InvalidElementStateException.class);
            try {
                return wait.until(drv -> {
                    final List<WebElement> foundElements = drv.findElements(by);
                    if (foundElements.size() > 0) {
                        for (WebElement element : foundElements) {
                            if (isElementPresentAndDisplayed(driver, element)) {
                                return true;
                            }
                        }
                    }
                    return false;
                });
            } catch (TimeoutException e) {
                return false;
            }
;

public by hamidmousavi created Feb 18, 2017  1585  0  4  0

Selenium TryFindElement

public void Test()
{
    IWebDriver driver = new FirefoxDriver();
    IWebElement element = null;
    if (TryFindElement(By.CssSelector("div.logintextbox"), out element)
    {
        bool visible = IsElementVisible(element);
        if  (visible)
        {
            // do something
        }
    }
}

public bool TryFindElement(By by, out IWebElement element)
{
    try
    {
        element = driver.FindElement(by);
    }
    catch (NoSuchElementException ex)
    {
        return false;
    }
    return true;
}

public bool IsElementVisible(IWebElement element)
{
    return element.Displayed && element.Enabled;
}
;

public by hamidmousavi created Feb 18, 2017  1555  0  5  0

This method will return WebElement After Given wait

// This method will return WebElement After Given wait           
public static WebElement FindWithWait(WebDriver driver,By locator, int seconds) throws Exception{
		// Sleep until the Element we want is visible or n seconds is over
		WebElement element=null;
		
		//Because if implict wait is set then fluint wait will not work
		driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		try{
					FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
							.withTimeout(seconds, TimeUnit.SECONDS)
							.pollingEvery(200, TimeUnit.MILLISECONDS)
							.ignoring(NoSuchElementException.class)
							.ignoring(StaleElementReferenceException.class)
							.ignoring(WebDriverException.class);
									 
				element=fluentWait.until(
							
							ExpectedConditions.visibilityOfElementLocated(locator)
							
							);
				return element;
					
		} catch(Exception e){
			
			throw new Exception("Timeout reached when searching for element! Time: " + seconds+" seconds " +"\n" +e.getMessage());
			
		}
		finally {
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		}
		
	}
;