1
votes

Je ne peux pas passer de variable à la méthode principale (concombre)

J'avais essayé de créer une méthode et de l'appeler d'un autre fichier à la classe principale mais cela ne fonctionnera pas le message d'erreur dit "java.lang.NullPointerException"

Main.class

WebDriver saddriver;

public void clickbyxpath (String xpathvalue) throws InterruptedException, IOException 
    {   
            WebDriverWait sad   =   new WebDriverWait(saddriver, 10); 

              //To wait for element visible
            System.out.println(xpathvalue);
            String x = xpathvalue;
            sad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(x)));

            wowdriver.findElement(By.xpath(x)).click();                 
    }

Method.class

Keywords kw = new Keywords();

@When("^gmailDD$") 
     public void gmailDD() throws Throwable{
     WebDriverWait wait5s = new WebDriverWait(driver, 5);
     String regis = "/html/body/div[2]/div[1]/div[5]/ul[1]/li[3]/a";
     String dd = "/html/body/div[1]/div/footer/div/div/div[1]";
     String empty = "/html/body/div[1]/div/footer";


     kw.clickbyxpath(regis);


     String handle= driver.getWindowHandle();
     System.out.println(handle);       
        // Store and Print the name of all the windows open               
        Set handles = driver.getWindowHandles();
        System.out.println("Log window id: "+handles);
        driver.switchTo().window("6442450949");

     kw.clickbyxpath(empty);   
     kw.clickbyxpath(dd);

}`

J'avais essayé de faire le même codage dans le même fichier, cela n'a pas de problème mais quand je déplace Method.class vers le nouveau fichier, le message d'erreur dit "java.lang.NullPointerException" mais je peux obtenir la valeur "xpathvalue".


4 commentaires

pouvez-vous s'il vous plaît poster votre trace de pile d'erreurs pour plus de détails. peut-être ne pourra-t-il pas trouver votre fenêtre.


le message d'erreur affiché à "kw.clickbyxpath (regis);" dans le fichier Main.class et la deuxième erreur s'affiche à "WebDriverWait sad = new WebDriverWait (saddriver, 10);" dans le fichier Method.class


J'avais lu ce journal des erreurs et il s'agit de WebDriver et WebDriverWait.


cela se produit car il ne trouvera pas votre instance de pilote.


3 Réponses :


1
votes

Cette erreur se produit car elle ne parvient pas à trouver votre instance de pilote.

reportez-vous à l'extrait de code ci-dessous. ce n'est pas un exemple de concombre, mais vous pouvez vous en faire une idée.

Method.class

package testing.framework;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Testing {
    public static WebDriver driver;

    public static void main(String[] args) {

        getWebDriver();
        String xpathValues= "//div[@class='FPdoLc VlcLAe']//input[@name='btnK']";
        Method m1 = new Method(driver);
        m1.clickByXpath(xpathValues);

    }

    public static void getWebDriver() {
        System.setProperty("webdriver.chrome.driver", "Your chrome driver path");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");
    }

}

Test. classe

package testing.framework;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Method {

    public WebDriver driver;
    WebElement _clickForSearch;
    public Method(WebDriver driver) {
        this.driver = driver;
    }
    public Method clickByXpath(String xpathValues) {
        WebDriverWait wait = new WebDriverWait(driver, 10);
        _clickForSearch = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpathValues)));
        _clickForSearch.click();    
        return this;
    }


}

Vous devez transmettre votre instance de pilote à une autre.


0 commentaires

1
votes

Je vous suggère donc de retirer l'attente du webdriver de votre méthode et de l'instancier lors de l'instanciation de votre webdriver. Je créerais ensuite des méthodes comme ceci:

Classe de pilote

driver.buttonClickByXpath(//YourXpathHere)

Méthode Click

Driver driver; 

Classe de test Importez votre classe de pilote

import Base.Driver;

Ensuite, vous aurez besoin de décrire votre classe de pilote comme ceci:

   public void buttonClickByXpath(String xpath) {
    try {
        WaitForPreseneOfElement(xpath);
        webDriver.findElement(By.xpath(xpath)).click();
    } catch (Exception e) {
        takeScreenshot();
        AllureLog("Failed to click on the button object. Please check your xpath. | xpath used = " + xpath + "");
        Assert.fail();

    }
}

Vous aurez maintenant accès à votre méthode en utilisant

private final String USER_DIRECTORY = System.getProperty("user.dir");
private final int GLOBAL_TIMEOUT = 30;
private WebDriver webDriver;
private WebDriverWait webDriverWait;


public Driver(String browserName) {
    this.browserName = browserName;
    System.out.println(browserName);
    switch (this.browserName.toUpperCase()) {

        case "CHROME":
            initializeChromeDriver();
            break;
    }
}
private void initializeChromeDriver() {
    System.setProperty("webdriver.chrome.driver", USER_DIRECTORY.concat("\\drivers\\chromedriver.exe"));
    webDriver = new ChromeDriver();
    webDriver.manage().window().maximize();
    webDriverWait = new WebDriverWait(webDriver, GLOBAL_TIMEOUT);
}


0 commentaires

0
votes

Le problème est "Méthode m1 = nouvelle méthode (pilote);" mot-clé, J'avais codé cette ligne en dehors de la méthode principale. merci beaucoup, Monsieur


0 commentaires