The Scenario is as follows:
When the code driver.get(url); runs, if the URL is not able to access (if server is not reachable) then it should throw an exception that server is not reachable and the code should stop executing the next line
driver.findElement(By.id("loginUsername")).sendKeys(username);
The following code I'm running in eclipse as follows:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
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;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class A {
static Properties p= new Properties();
String url=p.getProperty("url");
private static Logger Log = Logger.getLogger(A.class.getName());
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, InterruptedException {
WebDriver driver = new FirefoxDriver();
A a = new A();
Actions actions = new Actions(driver);
DOMConfigurator.configure("src/log4j.xml");
String url = a.readXML("logindetails","url");
String username = a.readXML("logindetails","username");
String password = a.readXML("logindetails","password");
//use username for webdriver specific actions
Log.info("Sign in to the OneReports website");
driver.manage().window().maximize();
driver.get(url);// In this line after opens the browser it gets the URL from my **D://NewFile.xml* file and try to open in the browser. If the Server is not reachable then it should stop here. else it takes the username and password from the same file then it will open the page in browser.
Thread.sleep(5000);
Log.info("Enter Username");
driver.findElement(By.id("loginUsername")).sendKeys(username);
Log.info("Enter Password");
driver.findElement(By.id("loginPassword")).sendKeys(password);
//submit
Log.info("Submitting login details");
waitforElement(driver,120 , "//*[@id='submit']");
driver.findElement(By.id("submit")).submit();
Thread.sleep(5000);
}
private static void waitforElement(WebDriver driver, int i, String string) {
// TODO Auto-generated method stub
}
public String readXML(String searchelement,String tag) throws SAXException, IOException, ParserConfigurationException{
String ele = null;
File fXmlFile = new File("D://NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(searchelement);
Node nNode = nList.item(0);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
ele=eElement.getElementsByTagName(tag).item(0).getTextContent();
}
return ele;
}
}
I tried this try block method as well. But it's not catching the exception just moving to the
driver.findElement(By.id("loginUsername")).sendKeys(username);
Error in code
line and I'm getting error as follows in the console:Unable to locate element: {"method":"id","selector":"loginUsername"} Command duration or timeout: 262 milliseconds
try
{
driver.get(url);
}
catch(Exception e)
{
Reporter.log("network server is slow..check internet connection");
Log.info("Unable to open the website");
throw new Error("network server is slow..check internet connection");
}