vendredi 4 août 2017

Selenium xpath causes randomly errors in selemium

The method approveAnswerByUsername is the problematic one. The method should check if the answer is approved or not. If isn't, should approve it.

The method is running weird because sometime recognize an approved answer and sometimes doesn't. In other cases recognize an unapproved one and doesn't recognized the second one even if it's almost the same thing.

I am guessing it's an xpath problem but i can't figure it out.

Here's the code that also contains the link and the credentials:

public class automate{
    WebDriver driver;

    @BeforeTest
    public void invokeBrowser() {
        try {

            System.setProperty("webdriver.chrome.driver",
                    "C:\\Users\\sanduc\\Desktop\\Selenium\\Kits\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
            driver.get("https://brainly.ro/");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test(priority = 1)
    public void log_in() {

        try {
            driver.findElement(
                    By.xpath("//div[@class='sg-content-box__actions']/nav[@class='brn-hero__navigation']/a[1]"))
                    .click();
            driver.findElement(By.xpath("//form[@action='/login?entry=2&return=/']/div[2]/input"))
                    .sendKeys("cristina-maria.sandu@brainly.com");
            driver.findElement(By.xpath("//form[@action='/login?entry=2&return=/']/div[3]/input"))
                    .sendKeys("Ctina.maria31");
            driver.findElement(By.xpath("//button[@type='submit']")).click();
            Thread.sleep(4000);
            driver.get("http://ift.tt/2fedB8b");

            scroll();

            int index_while = 1;

            while (true) {
                driver.get("http://ift.tt/2httzMq" + index_while);

                List<WebElement> allLinks = driver.findElements(By.xpath("//div[@class='task-content']/a"));
                // Total number of links in the webpage
                System.out.println("Total Links--> " + allLinks.size());

                if (allLinks.size() == 1) {
                    break;
                }

                Thread.sleep(4000);

                System.out.println(index_while);
                clickOnEachLink();
                index_while++;
            }
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }

    public void scroll() {
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,250)", "");
    }

    public void clickOnEachLink() {

        try {
            List<WebElement> allLinks = driver.findElements(By.xpath("//div[@class='task-content']/a"));
            int numberOfElementsFound = allLinks.size();
            for (int pos = 0; pos < numberOfElementsFound; pos++) {

                if (getElementWithIndex(By.xpath("//div[@class='task-content']/a"), pos).isDisplayed()) {

                    getElementWithIndex(By.xpath("//div[@class='task-content']/a"), pos).click();
                    System.out.println("pass");
                    System.out.println(pos);
                    Thread.sleep(4000);
                    approveAnswerByUsername("Merops");

                } else {
                    System.out.println("fail");
                    System.out.println(pos);
                }

            }
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
    }

    public WebElement getElementWithIndex(By by, int pos) {
        return driver.findElements(by).get((int) pos);
    }

    public void approveAnswerByUsername(String username) {
        try {

            WebElement btn_aproba = driver.findElement(By.xpath("//a[contains(.,'" + username
                    + "')]/ancestor::div[contains(@class, 'js-answer-element')]//div[@class='js-approve-answer']"));

            if (btn_aproba.getText().toLowerCase().contains("APROBAT".toLowerCase())) {
                System.out.println("Tema " + driver.getCurrentUrl() + " a fost deja aprobata");
                driver.navigate().back();

            } else {

                btn_aproba.click();
                Thread.sleep(2000);
                driver.navigate().refresh();
                System.out.println("Tema " + driver.getCurrentUrl() + " a fost aprobata acum");
                driver.navigate().back();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire