如何使Jenkins Testng套件失败,以触发Post Build,电子邮件通知

问题描述 投票:1回答:1

我在Jenkins服务器上安排了以下Testng程序。

它检查两个字符串中的一个,如果两个字符串都找不到,这意味着API失败了,我希望它提醒我。

但测试失败了,Jenkins在后期构建中生成电子邮件以提醒我的情况并没有严重失败。

它根据邮政编码和地址进行财务检查,如果是字符串“Great news!”或“谢谢”,如果两者都没有发现它失败,那么帖子构建将发出警报。

任何指针都非常感谢。

package Finance_check;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.io.IOException;

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

public class Check_Finance_Latest {

    static WebDriver driver;

    @Test
    public void check_finance() throws IOException, InterruptedException {

        // linux
        // System.setProperty("webdriver.chrome.driver",
        // "//usr//lib//chromium-browser//chromedriver");
        // System.setProperty("webdriver.chrome.driver",
        // "/home/user.name/Selenium/chromedriver");
        // driver = new ChromeDriver();

        // Windows
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\Chrome Driver\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().deleteAllCookies();

        driver.get("https://www.somesite.com/");

        // driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, 20);
        wait.until(ExpectedConditions.titleContains("CarShop | UK car supermarket | used cars for sale"));

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Accept Cookies')]")).click();

        Thread.sleep(8000);

        driver.findElement(By.xpath("//a[contains(@href,'/apply-for-finance')]")).click();

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Continue')]")).click();

        Thread.sleep(8000);

        Select title = new Select(driver.findElement(By.xpath("//select[contains(@name,'title')]")));
        title.selectByVisibleText("Dr");

        Thread.sleep(8000);

        WebElement fname = driver.findElement(By.xpath("//input[contains(@id,'firstname')]"));
        fname.sendKeys("Agent");

        Thread.sleep(8000);

        WebElement lname = driver.findElement(By.xpath("//input[contains(@id,'surname')]"));
        lname.sendKeys("Smith");

        Thread.sleep(8000);

        Select day = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_day')]")));
        day.selectByVisibleText("6");

        Thread.sleep(8000);

        Select month = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_month')]")));
        month.selectByVisibleText("January");

        Thread.sleep(8000);

        Select year = new Select(driver.findElement(By.xpath("//select[contains(@name,'dateOfBirth_year')]")));
        year.selectByVisibleText("1972");

        Thread.sleep(8000);

        WebElement phone = driver.findElement(By.xpath("//input[contains(@type,'tel')]"));
        phone.sendKeys("0207 485769");

        Thread.sleep(8000);

        WebElement email = driver.findElement(By.xpath("//input[contains(@id,'emailAddress')]"));
        email.sendKeys("[email protected]");

        Thread.sleep(8000);

        System.out.println("Filled in name / age details");
        driver.findElement(By.xpath("//button[contains(.,'Continue')]")).click();

        WebElement pcode = driver.findElement(By.xpath("//input[contains(@id,'postCode')]"));
        pcode.sendKeys("W1A 2HG");

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Find Address')]")).click();

        Thread.sleep(8000);

        Select address = new Select(driver.findElement(By.xpath("//select[contains(@ng-model,'addressKey')]")));

        Thread.sleep(8000);

        address.selectByVisibleText("7 The Road, LONDON");

        Thread.sleep(8000);

        driver.findElement(By.xpath("//button[contains(.,'Complete Pre-Application')]")).click();

        Thread.sleep(8000);

        try {
            String high = driver.findElement(By.xpath("//h4[contains(.,'Great news!')]")).getText();
            System.out.println("high string found");
        } catch (Exception e) {
        }

        try {
            String low = driver.findElement(By.xpath("//h4[contains(.,'Thank you.')]")).getText();
            System.out.println("low string found");
        } catch (Exception e) {
        }

        String credit_rating = driver.findElement(By.xpath("//h4")).getText();
        System.out.println("Credit Rating Result = " + credit_rating);

        if ("Great news!".equals(credit_rating)) {

            System.out.println("High Credit Found");

        } else if ("Thank you".equals(credit_rating)) {

            System.out.println("Low Credit Found");

        } else {
            System.out.println("Neither String found");

            try {
                throw new Exception("wheres my fiance message ? - fail!");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

                driver.quit();
            }
        }
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

}
java selenium jenkins webdriver testng
1个回答
1
投票

您应该使用TestNG Assert(http://static.javadoc.io/org.testng/testng/6.11/index.html?org/testng/Assert.html)进行测试。

而不是throw new Exception("wheres my fiance message ? - fail!"); 你可以使用fail("wheres my fiance message ? - fail!")

这将告诉TestNG您的测试失败了。

© www.soinside.com 2019 - 2024. All rights reserved.