在Selenium,我们需要获得ID" MANTPLAP300920141426403767374"从应用程序并将其存储在对象类型变量中。我们使用gettext()方法选择了ID。
我们需要使用不同的用户重新登录应用程序,并使用之前在另一个方法中的Object类型变量中保存的ID。我们要求通过在批准者应用程序的每个页面中进行比较来匹配ID(某些其他方法)。 如果找到匹配项,我们需要选中该框并批准该ID的记录。
流速: STep 1:以用户1登录创建记录并保存其ID。 2:由于它是一个字符串,我们将ID复制到Object变量中。 3:注销应用程序 4:使用不同用户登录以批准记录 5:如果在第一页中找不到,则搜索第一个用户在不同页面中创建的ID。
问题是:我们有一个Java类,我们成功存储了ID。在以不同的用户身份重新登录应用程序之后,我们不知道如何调用该对象类型变量并在该特定类中使用它。
在调试模式下,我们将NULL作为参考ID而不是保存的ID" MANTPLAP300920141426403767374"
我们想知道如何保存字符串ID并使用它来比较任何成员函数中的任何位置?
CommonFunction Library.java
public static void getSourceRefId(String object,String data)throws InterruptedException {
try{
{
ArrayList<String> NewTab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(NewTab.get(1));
String SourceRefId=driver.findElement(By.id(OR.getProperty(object))).getText();
System.out.println("the value is "+ SourceRefId);
Log.info(" Required source ref is present in the applicaiton"+ object);
}
} catch (NoSuchElementException e){
Log.error("Not able to get the source ref id--- " + e.getMessage());
driver.close();
}
}
public static void searchSourceRefId(String object,String data)throws InterruptedException {
try{
WindowsUtils.tryToKillByName("chromedriver.exe");
Thread.sleep(10000);
Log.info("Searching source ref id started");
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://tpl.test.open.url.com/portal/login.do");
driver.findElement(By.name("j_username")).sendKeys("appoascgval1");
driver.findElement(By.name("j_password")).sendKeys("Password1");
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//a[contains(text(),'Validation')]")).click();
Thread.sleep(30000);
//Getting the pages
List<WebElement> allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
System.out.println(allpages.size());
if(allpages.size()>0)
{
System.out.println("Pagination exists");
for(int i=0; i<=allpages.size(); i++)
{
//allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
List<WebElement> allrows = driver.findElements(By.xpath("//*[@id='content']/div[3]/table/tbody/tr"));
System.out.println("Total rows :" +allrows.size());
for(int row=1; row<=allrows.size(); row++)
{
System.out.println("Total rows :" +allrows.size());
allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
String validatorSoruceRefId = driver.findElement(By.xpath("//*[@id='content']/div[3]/table/tbody/tr["+row+"]/td[7]")).getText();
System.out.println(validatorSoruceRefId);
System.out.println("Row loop");
if(validatorSoruceRefId==Constants.SourceRefId){
// if(validatorSoruceRefId.contains("MANTPLAP300920141426403767374")){
System.out.println("is it found");
Log.info("Source id found" );
break;
}
else
{
System.out.println("Element doesn't exist");
// allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
}
}
System.out.println("I'm in page number" + i);
Thread.sleep(3000);
allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
allpages.get(i).click();
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
//System.out.println(i);
}
}
else
{
System.out.println("Pagination doesn't exists");
}
}catch (IndexOutOfBoundsException e) {
}
} }
请帮我解决这个问题。
答案 0 :(得分:0)
将第一个ID存储为静态变量并再次访问(以第二个用户身份登录后)。
答案 1 :(得分:0)
请试用此代码:
static String SourceRefId;
public static String getSourceRefId(String object,String data ) throws InterruptedException {
try{
ArrayList<String> NewTab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(NewTab.get(1));
SourceRefId=driver.findElement(By.id(OR.getProperty(object))).getText();
System.out.println("the value is "+ SourceRefId);
Log.info(" Required source ref is present in the applicaiton"+ object);
} catch (NoSuchElementException e){
Log.error("Not able to get the source ref id--- " + e.getMessage());
return SourceRefId;
}
}
public static void searchSourceRefId(String object,String data ) throws InterruptedException{
try{
WindowsUtils.tryToKillByName("chromedriver.exe");
Thread.sleep(10000);
//Log.info("Searching source ref id started");
WebDriver driver = null;
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://tpl.test.open.url.com/portal/login.do");
driver.findElement(By.name("j_username")).sendKeys("appoascgval1");
driver.findElement(By.name("j_password")).sendKeys("Password1");
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//a[contains(text(),'Validation')]")).click();
Thread.sleep(30000);
//Getting the pages
List<WebElement> allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
System.out.println(allpages.size());
if(allpages.size()>0)
{
System.out.println("Pagination exists");
for(int i=0; i<=allpages.size(); i++)
{
//allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
List<WebElement> allrows = driver.findElements(By.xpath("//*[@id='content']/div[3]/table/tbody/tr"));
System.out.println("Total rows :" +allrows.size());
for(int row=1; row<=allrows.size(); row++)
{
System.out.println("Total rows :" +allrows.size());
allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
String validatorSoruceRefId = driver.findElement(By.xpath("//*[@id='content']/div[3]/table/tbody/tr["+row+"]/td[7]")).getText();
System.out.println(validatorSoruceRefId);
System.out.println("Row loop");
if(validatorSoruceRefId.equals(SourceRefId)){
// if(validatorSoruceRefId.contains("MANTPLAP300920141426403767374")){
System.out.println("is it found");
Log.info("Source id found" );
break;
}
else
{
System.out.println("Element doesn't exist");
// allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
}
}
System.out.println("I'm in page number" + i);
Thread.sleep(3000);
allpages = driver.findElements(By.xpath("//div[@class='content-table-pagination']//a"));
allpages.get(i).click();
driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
//System.out.println(i);
}
}
else
{
System.out.println("Pagination doesn't exists");
}
}catch (IndexOutOfBoundsException e) {
}
}
在上面的代码中,我做了以下更改:
的 强>
希望这对你有用。