I am trying to use Selenium for website automation testing tasks and I am new to Selenium testing framework. The the situation I faced is to wait the website components loading and then do the related operations with the target components. The WaitFor
method with xpath
and waitingTimes
input parameters are designed as below.
The experimental implementation
private static bool WaitFor(IWebDriver driver, string xpath, uint waitingTimes = 100)
{
log.Info("Wait for " + xpath);
uint count = 0;
while (IsElementExists(driver, xpath) == false)
{
if (count >= waitingTimes)
{
return false;
}
Thread.Sleep(1000);
count++;
}
return true;
}
private static bool IsElementExists(IWebDriver driver, string xpath)
{
try
{
var element = driver.FindElement(By.XPath(xpath));
return true;
}
catch (Exception ex)
{
log.Info(ex.Message);
}
return false;
}
The usage of WaitFor
method:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Repository.Hierarchy;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace WaitForMethodSeleniumWebDriver
{
class Program
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static BackgroundWorker backgroundWorkerForLogging = new BackgroundWorker();
static void Main(string[] args)
{
LogHandler();
log.Info("Main method");
IWebDriver driver = new ChromeDriver(@".\");
driver.Url = "https://codereview.stackexchange.com/";
WaitFor(driver, ".//*[@id='content']");
// perform next step operation
// ...
driver.Close();
driver.Quit();
}
// Reference: https://stackoverflow.com/a/4172968/6667035
private static void LogHandler()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(System.Reflection.Assembly.GetEntryAssembly());
XmlConfigurator.Configure(hierarchy, new FileInfo("log4net.config"));
FileAppender fileAppender = new FileAppender();
fileAppender.AppendToFile = true;
fileAppender.LockingModel = new FileAppender.MinimalLock();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder = stringBuilder.Append("log").Append(DateTime.UtcNow.ToString("_MM-dd-yyyy_hh_mm_ss")).Append(".txt");
fileAppender.File = stringBuilder.ToString();
log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout();
pl.ConversionPattern = "%d [%2%t] %-5p [%-10c] %m%n";
pl.ActivateOptions();
fileAppender.Layout = pl;
fileAppender.ActivateOptions();
BasicConfigurator.Configure(fileAppender);
// Reference: https://stackoverflow.com/a/2077767/6667035
backgroundWorkerForLogging.DoWork += (sender, e) =>
{
UDPListener();
};
// Start BackgroundWorker
backgroundWorkerForLogging.RunWorkerAsync();
}
// launch this in a background thread
private static void UDPListener()
{
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
var udpClient = new System.Net.Sockets.UdpClient(10001);
while (true)
{
var buffer = udpClient.Receive(ref remoteEndPoint);
var loggingEvent = System.Text.Encoding.Unicode.GetString(buffer);
Console.WriteLine(loggingEvent);
}
}
private static bool WaitFor(IWebDriver driver, string xpath, uint waitingTimes = 100)
{
log.Info("Wait for " + xpath);
uint count = 0;
while (IsElementExists(driver, xpath) == false)
{
if (count >= waitingTimes)
{
return false;
}
Thread.Sleep(1000);
count++;
}
return true;
}
private static bool IsElementExists(IWebDriver driver, string xpath)
{
try
{
var element = driver.FindElement(By.XPath(xpath));
return true;
}
catch (Exception ex)
{
log.Info(ex.Message);
}
return false;
}
}
}
All suggestions are welcome.