如何使 Jenkins Testng 套件严重失败以触发 Post 构建,电子邮件通知

How to make a Jenkins Testng suite fail badly to trigger Post Build , email notification

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

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

但是测试失败了,在 post 构建中,Jenkins 生成电子邮件提醒我的失败还不够严重。

它根据 post 代码和地址进行财务检查,如果字符串 "Great news!" 或 "Thank you" 均未找到则失败,因此 post构建将发出警报。

非常感谢任何指点。

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("ITTestingDonotContact@example.com");

        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();
    }

}

您应该使用 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 您的测试正确地失败了。