Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SeleniumWebDriver和Java将一个类值传递给另一个类?_Java_Selenium_Selenium Webdriver - Fatal编程技术网

如何使用SeleniumWebDriver和Java将一个类值传递给另一个类?

如何使用SeleniumWebDriver和Java将一个类值传递给另一个类?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我有一门课,如下: package keya; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; public class Module3 extends Common{ public static void main(String...strings) throws Exceptio

我有一门课,如下:

package keya;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

public class Module3 extends Common{

    public static void main(String...strings) throws Exception{
        Common c = new Common();
        driver.get("webAddress");
        driver.findElement(By.xpath("//*[@id='Username']")).sendKeys("A1");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("1");
        driver.findElement(By.xpath("//*[@id='loginBox']/form/p/button")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='mainNav']/li[2]/a")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='addNewEntryButton']")).click();
        WebElement dropdown = driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select"));
        List<WebElement> dropOptions = dropdown.findElements(By.tagName("Option"));
        for (int i=0; i<dropOptions.size(); i++)
        {
         System.out.println(dropOptions.get(i).getText());
        }
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select")).sendKeys(Keys.ARROW_DOWN);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[5]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[6]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='saveEntryButton']")).click();
        Thread.sleep(5000L);
        driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div/div[3]/div/header/div/button[4]")).click();
        //Here is the verification whether Submit button is present or not
        c.isElementPresent(By.xpath("//button[contains(.,'submit')]"));
        //Rest of the code is here
    }
}
公共类模块3{

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
findElement1 a = new findElement1();
driver.get("webAddress");
driver.findElement(By.xpath("//*[@id='Username']")).sendKeys("A1");
driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("1");
driver.findElement(By.xpath("//*[@id='loginBox']/form/p/button")).click();
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='mainNav']/li[2]/a")).click();
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='addNewEntryButton']")).click();


WebElement dropdown = driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select"));
List<WebElement> dropOptions = dropdown.findElements(By.tagName("Option"));
for (int i=0; i<dropOptions.size(); i++)
{
 System.out.println(dropOptions.get(i).getText());
}
Thread.sleep(2000L);
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select")).sendKeys(Keys.ARROW_DOWN);
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[5]/input")).sendKeys("5");
driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[6]/input")).sendKeys("5");
driver.findElement(By.xpath("//*[@id='saveEntryButton']")).click();

Thread.sleep(5000L);

driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div/div[3]/div/header/div/button[4]")).click();

    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    a.findValue();
    driver.findElement(By.xpath("//button[contains(.,'submit')]")).click();


    }
}


我想将class2的操作执行到class1中,其中class1的值位于所提到的a.findValue位置;是否可以将class1的驱动程序值传递到class2

确定。只需从第二个类中删除WebDriver的static关键字,并使用WebDriver创建一个setter或构造函数


在第一个类中实例化它,并使用setter或构造函数设置WebDriver。

创建一个公共类,其中可以保存公共内容并用于任何类:

package keya;    
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.By;

public class Common {
    protected static WebDriver driver;

    public Common(){
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }

    public boolean isElementPresent(By by){
        try{
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e){
            return false;
        }
    }
}
在模块3中测试时,代码应如下所示:

package keya;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

public class Module3 extends Common{

    public static void main(String...strings) throws Exception{
        Common c = new Common();
        driver.get("webAddress");
        driver.findElement(By.xpath("//*[@id='Username']")).sendKeys("A1");
        driver.findElement(By.xpath("//*[@id='Password']")).sendKeys("1");
        driver.findElement(By.xpath("//*[@id='loginBox']/form/p/button")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='mainNav']/li[2]/a")).click();
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='addNewEntryButton']")).click();
        WebElement dropdown = driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select"));
        List<WebElement> dropOptions = dropdown.findElements(By.tagName("Option"));
        for (int i=0; i<dropOptions.size(); i++)
        {
         System.out.println(dropOptions.get(i).getText());
        }
        Thread.sleep(2000L);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[1]/select")).sendKeys(Keys.ARROW_DOWN);
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[5]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='timeEntryTable']/tbody/tr[1]/td[6]/input")).sendKeys("5");
        driver.findElement(By.xpath("//*[@id='saveEntryButton']")).click();
        Thread.sleep(5000L);
        driver.findElement(By.xpath("html/body/div[2]/div[2]/div[2]/div/div[3]/div/header/div/button[4]")).click();
        //Here is the verification whether Submit button is present or not
        c.isElementPresent(By.xpath("//button[contains(.,'submit')]"));
        //Rest of the code is here
    }
}

因此,您可以对任意数量的类/模块使用公共类的isElementPresent方法。您可以通过扩展公共类来编写更多的类,这样您就可以使用一些公共方法,WebDriver只在公共类中实例化一次。

是的,您可以通过使用构造函数来实现。正如我所理解的,您希望在类中保留一些公共内容,例如元素、文本、,WebDriver实例等,可在需要时从其他类中使用。如果我正确理解您的需求,请让我知道。上面的代码是作为java应用程序编写的。如果您使用TestNG或JUnit作为验证工具,代码可能会更有条理。对于Module3:您已经编写了扩展公共类的Module3类。若您喜欢测试另一个,那个么可以编写模块4扩展公共,并可以使用公共类中的公共内容。