繁体   English   中英

从一个类访问同一包中的另一个类的方法

[英]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();

是你所需要的。

但是,我建议CreateCustomerAdminLogin是一个类中的两个独立函数,比如Admin ,因为它们是管理员可以执行的操作(如果我理解你的任务是正确的)。 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM