我需要你的帮助。让我来形容你的情景。
我有一个登录表单和10+电子邮件,相同的密码。我必须编写一个自动脚本,其中包含一个电子邮件ID和密码并输入登录表单。如果电子邮件ID正确,则允许用户进入应用程序并显示“注销”链接。如果电子邮件ID不正确,则显示消息“输入正确的电子邮件ID和密码“。 现在问题是我如何写(IF条件)来检查允许登录和不允许登录,因为如果假设我写了如果条件不允许登录并输入登录ID和密码是正确的,它显示错误消息由于IF条件失败。
我试着写代码:
enter code here
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginCheckofProcuretiger {
public static WebDriver driver;
public static WebElement web;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
driver.navigate().to("https://eprocure.procuretiger.com/EPROCP");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
//driver.close();
}
@Test
public void test() {
String Mytest[] ={
"bidder1A@abcprocure.com",
"bidderAB@abcprocure.com",
"bidderAC@abcprocure.com",
"bidderAD@abcprocure.com",
"bidderAE@abcprocure.com",
"bidderAF@abcprocure.com",
"bidderDK@abcprocure.com",
"bidderDL@abcprocure.com",
"bidderDM@abcprocure.com"
};
int i;
for( i=0;i<Mytest.length;i++ ){
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("j_username")).sendKeys(Mytest[i]);
driver.findElement(By.id("j_password")).sendKeys("auction@123");
driver.findElement(By.name("btnlogin")).click();
if(driver.findElement(By.xpath(".//fieldset/div[@class='errorMsg t_space']")).getText() != null)
{
System.out.println("User Not allowed to access. " +Mytest[i]);
}
else
{
driver.findElement(By.linkText("Logout")).click();
driver.findElement(By.linkText("Home")).click();
System.out.println("User allowed to access. " +Mytest[i]);
}
答案 0 :(得分:1)
而不是if-else,您需要使用try / catch并仅测试Logout链接的存在。没有测试过这段代码,但我认为它应该可行。
try{
driver.findElement(Logout link locator);
System.out.println("User is allowed access");
.....
}catch(Exception e){
System.out.println("User is NOT allowed access");
.....
}finally{
.....
}
答案 1 :(得分:0)
我更喜欢使用它(见下文),因为使用try{}catch{}
elem = driver.findElements(locator);
if (elem.Count > 0)
{
//Allowed
}
else
{
//Not allowed
}
答案 2 :(得分:0)
使用(1)try / catch for findElement或(2)count&gt; 0 for findElements
非常慢(而且不是很优雅)最好根据具体情况编写支票
例如,假设您的登录表单具有
(1) <input class="can_login"> or (2) <input class="no_login">
为了比try / catch of count&gt; 0做更快的检查,做一些像(在Python中):
list_of_input_classes = [ elem.get_attribute('class') for elem in driver.find_elements_by_css_selector("form#login > input") ]
if "can_login" in list_of_input_classes:
#you can login
elif "no_login" in list_of_input_classes:
#you can't login
else:
raise Exception( "why were neither of these options found? i.e. unexpected result" )
答案 3 :(得分:0)
我使用了这段代码及其工作
try{
driver.findElement(By.linkText("Login")).click();
driver.findElement(By.id("j_username")).sendKeys(LoginIDs[i]);
driver.findElement(By.id("j_password")).sendKeys(Password[i]);
driver.findElement(By.name("btnlogin")).click();
driver.findElement(By.linkText("Logout")).click();
} catch(Exception e) {
System.out.println("Login with "+ LoginIDs[i] + " : Failed");
} finally {
driver.findElement(By.linkText("Home")).click();
}
答案 4 :(得分:0)
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
public class ExportinExcel {
public static WebDriver driver;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//driver=new FirefoxDriver();
// driver.navigate().to("URL");
//driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Test
public void test() throws IOException, RowsExceededException, WriteException {
//fail("Not yet implemented");
// Given the path where to store Excel.
File FExcel = new File("D:\\software\\Excel\\createExcel.xls");
/* Create a Workbook. */
WritableWorkbook workbookexcel= Workbook.createWorkbook(FExcel);
/* Create a Worksheet. */
workbookexcel.createSheet("Data", 0);
WritableSheet writeablesheet= workbookexcel.getSheet(0);
/* Add Content in row and column and here coumn value increment each time. */
int i=0,j=0;
for(;j<tablecontent.lenght();j++){
jxl.write.Label Data1 = new jxl.write.Label(i, j, "Enter Path of Table data");
writeablesheet.addCell(Data1);}
/*jxl.write.Label Data2 = new jxl.write.Label(i, j, "Enter Path of Table data");
writeablesheet.addCell(Data2);*/
workbookexcel.write();
workbookexcel.close();
}
}**
答案 5 :(得分:0)
accept
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import jxl.Workbook;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Label;
public class ExportinExcel {
public static WebDriver driver;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//driver=new FirefoxDriver();
// driver.navigate().to("http://www.indianrail.gov.in/tatkal_Scheme.html");
//driver.manage().timeouts().implicitlyWait(100, TimeUnit.MILLISECONDS);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
driver.quit();
}
@Test
public void test() throws IOException, RowsExceededException, WriteException {
//fail("Not yet implemented");
// Given the path where to store Excel.
File FExcel = new File("D:\\software\\Excel\\createExcel.xls");
/* Create a Workbook. */
WritableWorkbook workbookexcel= Workbook.createWorkbook(FExcel);
/* Create a Worksheet. */
workbookexcel.createSheet("Data", 0);
WritableSheet writeablesheet= workbookexcel.getSheet(0);
/* Add Content in row and column and here coumn value increment each time. */
jxl.write.Label Data1 = new jxl.write.Label(0, 0, driver.findElement(By.xpath(".//tr[1]/td[1]/p/b/span")).gettext() );
writeablesheet.addCell(Data1);
jxl.write.Label Data2 = new jxl.write.Label(0, 1, driver.findElement(By.xpath(".//tr[1]/td[2]/p/b/span")).gettext() );
writeablesheet.addCell(Data2);
jxl.write.Label Data3 = new jxl.write.Label(0, 2, driver.findElement(By.xpath(".//tr[1]/td[3]/p/b/span")).gettext() );
writeablesheet.addCell(Data3);
jxl.write.Label Data4 = new jxl.write.Label(1, 0, driver.findElement(By.xpath(".//tr[2]/td[1]/p/b/span")).gettext() );
writeablesheet.addCell(Data4);
jxl.write.Label Data5 = new jxl.write.Label(1, 1, driver.findElement(By.xpath(".//tr[2]/td[2]/p/b/span")).gettext() );
writeablesheet.addCell(Data5);
jxl.write.Label Data6 = new jxl.write.Label(1, 2, driver.findElement(By.xpath(".//tr[2]/td[3]/p/b/span")).gettext() );
writeablesheet.addCell(Data6);
workbookexcel.write();
workbookexcel.close();
}
/html/body/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[3]/td/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[1]/td[1]/table/tbody/tr[4]/td/table/tbody/tr[2]/td[1]/p/b/span
.//tr[1]/td[1]/p/b/span
.//tr[1]/td[2]/p/b/span