我的脚本每次都没有成功运行。有时运行后它会成功运行 1 次,下次我再次运行时它会失败。包示例;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Script1
{
public static void main(String args[])throws Exception
{
//Take the data from input xls file
FileInputStream fi =new FileInputStream("D:\\selenium_test\\Object
Repositoty\\Input\\Login1.xls");
Workbook wb = Workbook.getWorkbook(fi);
Sheet sh = wb.getSheet(0);
//Create the Result xls file
FileOutputStream fo = new FileOutputStream("D:\\selenium_test\\Object
Repositoty\\Output\\Login_Results1.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("Results", 0);
Thread.sleep(20000);
String msg = "";
WebDriver driver = new InternetExplorerDriver();
driver.get("https://www.700creditsolution.com");
driver.manage().window().maximize();
Set<String> windowids = driver.getWindowHandles();
Iterator<String> iter= windowids.iterator();
for (int i = 1; i < sh.getRows(); i++)
{
while(iter.hasNext())
{
System.out.println("Main Window ID : "+iter.next());
}
driver.findElement(By.id("lgnLogin_UserName")).clear();
driver.findElement(By.id("lgnLogin_UserName")).sendKeys(sh.getCell(0,
i).getContents());
driver.findElement(By.id("lgnLogin_Password")).clear();
driver.findElement(By.id("lgnLogin_Password")).sendKeys(sh.getCell(1,
i).getContents());
driver.findElement(By.id("lgnLogin_LoginButton")).click();
Thread.sleep(5000L);
windowids = driver.getWindowHandles();
iter= windowids.iterator();
String main_windowID=iter.next();
String tabbed_windowID=iter.next();
System.out.println("Main Window ID :" +main_windowID);
Thread.sleep(2000);
//switch over to pop-up window
driver.switchTo().window(tabbed_windowID);
System.out.println("Pop-up window Title : "+driver.getTitle());
driver.findElement(By.xpath("//*[@id='BtnClose']")).click();
Thread.sleep(1000);
driver.switchTo().window(main_windowID);
msg= driver.findElement(By.xpath("//table[@id='lgnLogin']/tbody/tr/td/table
/tbody/tr[4]/td")).getText();
System.out.println(msg);
Thread.sleep(2000);
// driver.get("https://www.700creditsolution.com");
// Thread.sleep(2000);
for (int j = 0; j < sh.getColumns(); j++)
{
Label lab=new Label(j,i,sh.getCell(j, i).getContents());
ws.addCell(lab);
Label result = new Label(2,i, msg);
ws.addCell(result);
String c2String = sh.getCell(2, i).getContents().toString();
if(c2String.equalsIgnoreCase(msg))
{
Label status=new Label(3,i,"Pass");
ws.addCell(status);
}
else
{
Label status=new Label(3,i,"Fail");
ws.addCell(status);
}
}
}
Label un=new Label(0,0,"User ID");
Label pw=new Label(1,0,"Password");
Label rs=new Label(2,0,"Results");
Label st=new Label(3,0,"Status");
ws.addCell(un);
ws.addCell(pw);
ws.addCell(rs);
ws.addCell(st);
wwb.write();
wwb.close();
driver.quit();
}
}
请尽快帮助我解决这个问题。在我的系统中我有windows7,IE9。我的应用程序最好只在 IE 中支持。帮助将不胜感激。