[英]Accessing method from one class to another class in same package
我在同一個包“testing”中有兩個名為 AdminLogin 和 CreateCustomer 的類。 我在 AdminLogin.java 中編寫了一個方法 commonLogin() 用於登錄目的。所以我需要在 CreateCustomer 類中調用 commonLogin(),而不是編寫相同的登錄代碼。我該怎么做? 有哪位請告訴我。 AdminLogin.java:
package testing;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AdminLogin {
public static void commonLogin()
{
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
}
public static void main(String[] args) {
AdminLogin .commonLogin();
}
}
和 CreateCustomer.java
package testing;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CreateCustomer {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new FirefoxDriver();
// And now use this to visit BluBilling
driver.get("http://testing.blubilling.in");
//Fetching the username and password
WebElement element = driver.findElement(By.id("j_username"));
// Enter something to search for
element.sendKeys("xxxx");
WebElement element1 = driver.findElement(By.id("j_password"));
element1.sendKeys("zzzz");
// Entering into bluBilling application
element.submit();
//Creating a Customer
driver.navigate().to("https://testing.blubilling.in/customer /createCustomer2");
//Entering the details of the Customer
WebElement element4 = driver.findElement(By.id("name"));
element4.sendKeys("");
WebElement element5 = driver.findElement(By.id("firstName"));
element5.sendKeys("chris");
WebElement element6 = driver.findElement(By.id("lastName"));
element6.sendKeys("broad");
WebElement element7 = driver.findElement(By.id("emailPrimary"));
element7.sendKeys("chris@blusyn.com");
WebElement element8 = driver.findElement(By.id("username"));
element8.sendKeys("");
WebElement element9 = driver.findElement(By.id("password"));
element9.sendKeys("Chris1234");
WebElement element10 = driver.findElement(By.id("confirm"));
element10.sendKeys("Chris1234");
driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);
//Saving the Customer details
WebElement element11 = driver.findElement(By.cssSelector("input[value='Save']"));
element11.click();
}
}
在CreateCustomer 中:
AdminLogin.commonLogin();
是你所需要的。
但是,我建議CreateCustomer和AdminLogin是一個類中的兩個獨立函數,比如Admin ,因為它們是管理員可以執行的操作(如果我理解你的任務是正確的)。 祝你好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.