I have a program that loops through a file of IP addresses which serve as the URLs. It logs in, makes changes, saves, and logs out. I need to implement a pass or fail test to see which URLs do not complete the loop. In addition, I also need the loop to continue passed the failures while writing them to a file so I can see which did not pass. Right now, program will loop until it encounters a bad URL then throw a ERR_CONNECTION_TIMED_OUT. Any help would be appreciated as I am new to programming and SO.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class IPLog {
WebDriver driver;
private BufferedWriter bwr1;
//Sign In
public void userLogin() throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
String data = "";
try {
BufferedReader reader = new BufferedReader(new FileReader("path"));
String line;
while((line = reader.readLine()) != null) {
data ="http://" line;
System.out.println(data);
//open browser
driver.get(data);
driver.switchTo().frame(driver.findElement(By.name("bodyf")));
Thread.sleep(2000);
driver.findElement(By.name("username")).sendKeys("user");
Thread.sleep(2000);
driver.findElement(By.name("password")).sendKeys("pass");
driver.findElement(By.xpath("/html/body/form/span/table/tbody/tr[3]/td/p/input")).click();
//find password
driver.switchTo().frame(driver.findElement(By.name("left")));
driver.findElement(By.xpath("//*[@id=\"4050000\"]/a")).click();
driver.findElement(By.xpath("//*[@id=\"4050000\"]/table/tbody/tr[3]/td[2]/a")).click();
//change password
driver.switchTo().parentFrame();
driver.switchTo().frame(1);
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[1]/table/tbody/tr[2]/td[2]/input")).sendKeys("newpass");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[1]/table/tbody/tr[3]/td[2]/input")).sendKeys("newpass");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[2]/input[2]")).click();
//save and logout
Thread.sleep(3000);
driver.switchTo().alert().accept();
driver.switchTo().parentFrame();
driver.switchTo().frame(driver.findElement(By.name("left")));
driver.findElement(By.id("sysLogout")).click();
Thread.sleep(3000);
driver.switchTo().alert().accept();
//completion test
FileWriter writer = new FileWriter("path");
BufferedWriter bwr = (new BufferedWriter(writer));
bwr.write(data "- Test Passed\n");
bwr.write("\n");
try {
FileWriter writer1 = new FileWriter("path");
bwr1 = (new BufferedWriter(writer1));
bwr1.write(data "- Test Failed");
bwr1.write("\n");
System.out.println("fail");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
IPLog obj = new IPLog();
obj.userLogin();
}
}
CodePudding user response:
What you can do is add a try-catch for each line you are processing. For instance, you can rewrite your loop as follows:
FileWriter writer = new FileWriter("path");
BufferedWriter bwr = (new BufferedWriter(writer));
while((line = reader.readLine()) != null) {
data ="http://" line;
System.out.println(data);
try {
doActionLine(data);
//add test passed
bwr.write(data "- Test Passed\n");
bwr.write("\n");
} catch (Exception e) {
//add test failed
bwr.write(data "- Test Failed\n");
bwr.write("\n");
}
}
As you can see, doActionLine funcion has encapsulated the checks:
private void doActionLine(String data) throws InterruptedException {
//open browser
driver.get(data);
driver.switchTo().frame(driver.findElement(By.name("bodyf")));
Thread.sleep(2000);
driver.findElement(By.name("username")).sendKeys("user");
Thread.sleep(2000);
driver.findElement(By.name("password")).sendKeys("pass");
driver.findElement(By.xpath("/html/body/form/span/table/tbody/tr[3]/td/p/input")).click();
//find password
driver.switchTo().frame(driver.findElement(By.name("left")));
driver.findElement(By.xpath("//*[@id=\"4050000\"]/a")).click();
driver.findElement(By.xpath("//*[@id=\"4050000\"]/table/tbody/tr[3]/td[2]/a")).click();
//change password
driver.switchTo().parentFrame();
driver.switchTo().frame(1);
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[1]/table/tbody/tr[2]/td[2]/input")).sendKeys("newpass");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[1]/table/tbody/tr[3]/td[2]/input")).sendKeys("newpass");
Thread.sleep(2000);
driver.findElement(By.xpath("/html/body/form/p[2]/input[2]")).click();
//save and logout
Thread.sleep(3000);
driver.switchTo().alert().accept();
driver.switchTo().parentFrame();
driver.switchTo().frame(driver.findElement(By.name("left")));
driver.findElement(By.id("sysLogout")).click();
Thread.sleep(3000);
driver.switchTo().alert().accept();
}
I hope this help you.