小编Ani*_*udh的帖子

如何在 testNG 中使用 IAnnotationTransformer?

如何在 testNG 中使用 IAnnotationTransformer?当我调试时,代码从未进入转换函数。它执行了所有 3 个测试。我使用 Maven 来触发我的测试,这就是我所拥有的——

public class SomeTest {

    @BeforeClass
    public void before(){

        TestNG testNG = new TestNG();
        testNG.setAnnotationTransformer(new Transformer());
    }

    @Test(priority = 1)
    public void test1(){}

    @Test(priority = 2)
    public void test2(){}

    @Test(priority = 3)
    public void test3(){}
}

public class Transformer implements IAnnotationTransformer {

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){

        if (true){
            annotation.setEnabled(false);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java testng selenium annotations

5
推荐指数
1
解决办法
8709
查看次数

对于printwriter对象,flush的用途是什么?

我正在寻找理论分析.我的意思是,缓冲系统如何工作以及使用flush提供什么优势?如果可能的话,请举例说明.

java io printwriter

5
推荐指数
1
解决办法
7378
查看次数

使用具有特定mysql主机的远程主机上的SSH隧道连接到MySql数据库

我正在使用Jsch连接到远程mysql数据库,其中ssh主机与mysql主机不同,如下图的上半部分所示: 在此输入图像描述

以下是我用于SSH连接的代码:

private static void connectSSH() throws SQLException {
    try {
        java.util.Properties config = new java.util.Properties();
        JSch jsch = new JSch();
        jsch.setLogger(new MyLogger());
        session = jsch.getSession(sshUser, sshHost, 22);
        jsch.addIdentity(SshKeyFilepath, sshPassword);
        config.put("StrictHostKeyChecking", "no");
        config.put("ConnectionAttempts", "3");
        session.setConfig(config);
        session.connect();

        System.out.println("SSH Connected");

        Class.forName(driverName).newInstance();

        int assinged_port = session.setPortForwardingL(localPort, remoteHost, remotePort);

        System.out.println("localhost:" + assinged_port + " -> " + sshHost + ":" + remotePort);
        System.out.println("Port Forwarded");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

并最终使用以下代码连接到数据库:

 Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:" + localPort, …
Run Code Online (Sandbox Code Playgroud)

java mysql database ssh jdbc

5
推荐指数
1
解决办法
3677
查看次数

测试日志输出 Java Spring

我有一个在 java 10 上运行的 spring boot 2 应用程序,使用 SLF4J 和 logback 作为底层记录器。

给定以下弹簧组件:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MyClass {

    private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);

    public void hello() {
        // more logic...
        LOG.trace("World");
    }
}
Run Code Online (Sandbox Code Playgroud)

以及希望验证日志输出(想象一个更复杂的场景,其中日志将包含变量和上下文信息)或生成日志消息的事实被认为是关键的测试。

import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.test.rule.OutputCapture;

import static org.hamcrest.Matchers.containsString;

public class MyClassTest {

    private final MyClass myClass = new MyClass();

    @Rule
    public final OutputCapture outputCapture = new OutputCapture();

    @Test
    public void successShouldLogSuccessMessages() {
        myClass.hello();
        outputCapture.expect(containsString("World"));
    }
}
Run Code Online (Sandbox Code Playgroud)

要通过此测试,必须将日志级别设置为 …

java junit logback slf4j spring-boot-test

5
推荐指数
1
解决办法
2万
查看次数

Internet Explorer 8-10中的Selenium WebDriver Windows切换问题

我发现在尝试使用Selenium WebDriver测试我们的应用程序时遇到了问题.问题在于IE9中不稳定的弹出窗口.它并不总是可重现的,它发生在大约20%的窗口切换中,但几乎不可能在IE上进行测试.在FireFox中,一切都很完美.

  1. 我尝试增加超时:

TimeSpan interval = new TimeSpan(0, 0, 10); driver.Manage().Timeouts().ImplicitlyWait(interval);

  1. 为对象查找创建自己的方法:

               for (int x = 0; x <= waitTimeOut; x++)
                {
                    try
                    {
                        element = (IWebElement)driver.FindElement(By.XPath(obj.Xpath));
                        return element;
                    }
    
                    catch{}
                }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 尝试使用CssSelecotrs

  3. 在找到元素之前尝试进行一些重新切换:

    driver.SwitchTo().Window(GetWindowHandle(2, 1)); driver.SwitchTo().Window(GetWindowHandle(0, 1)); driver.SwitchTo().Window(GetWindowHandle(2, 1));

如果出现问题,它始终只发生在我尝试在页面上找到的第一个元素.如果找到该元素,则在此页面上查找其他元素没有任何问题.所以我认为问题在于聚焦.

调试器中的Windows句柄正确显示.例如,如果我切换到第三个窗口,driver.CurrentWindowHandle给我第三个窗口的正确句柄.但是如果我试图找到任何元素,FindElement()会抛出异常.页面已加载,我可以手动单击该元素,但FindElement()无法找到它.如果我重新运行测试,则可以毫无问题地通过此步骤,并且仅在下一次切换或进一步切换时失败.这是不可预测的.

这样的问题可能是什么原因?

java selenium internet-explorer webdriver

4
推荐指数
1
解决办法
8595
查看次数

无法使用selenium webdriver单击/鼠标悬停在元素上

1.转到此URL: - > http://wallethub.com/profile/test_insurance_company/

2.在页面的右侧,将鼠标悬停在星星上并点击第五个星形代码实际应该(1)进行悬停和(2)确保当你将鼠标悬停在它们上面时,里面的星星点亮,然后(3) )点击第五颗星.

以下不起作用

 import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.interactions.Actions;

public class Wallethub2 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ProfilesIni prof = new ProfilesIni();
        FirefoxProfile fp = prof.getProfile("sel");

        WebDriver driver=new FirefoxDriver(fp);

        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://wallethub.com/profile/test_insurance_company/");

//      Xpaths of 5 stars
       String star1="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][1]";
       String star2="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][2]";
       String star3="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][3]";
       String star4="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][4]";
       String star5="//div[@class='wh-rating-choices-holder']/a[contains(@href, '#')][5]";

        WebElement srating1=driver.findElement(By.xpath(star1));            
        WebElement srating2=driver.findElement(By.xpath(star2));            
        WebElement srating3=driver.findElement(By.xpath(star3));            
        WebElement …
Run Code Online (Sandbox Code Playgroud)

java selenium webdriver

3
推荐指数
1
解决办法
9233
查看次数

Java如果Else语句错误

我试图根据用户的输入确定要计算的公式.当它们都被{}关闭时它起作用,但是当整个语句关闭时,我的System.out.print不能识别它们.目前if语句周围没有括号.并且它声明已在主字符串中定义了totalSalary.感谢您的时间和意见.

    if(annualSales >= 96000)
    {
     double totalSalary = annualPay + advCommission;
    if(annualSales <= 95999)
    { 
     double totalSalary = annualPay + commission;



    //prints table for display   
    //Columns
    System.out.print("SalesPerson\t");
    System.out.print("AnnualSales\t");
    System.out.print("Commission\t "); 
    //Columns
    System.out.print("Total Salary\t");System.out.println("Difference from user");

    //user input
    System.out.print(firstName);System.out.print("\t\t");
    System.out.println(annualSales); System.out.print(commission); 
    System.out.print(totalSalary);System.out.println("Difference from user");
    //preset 1
    System.out.print(presetSalesPerson[0]);
    System.out.print("\t\t");
    System.out.println(presetAnnualSales[0]);             System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");
//preset 2
    System.out.print(presetSalesPerson[1]);System.out.print("\t\t");
     System.out.println(presetAnnualSales[1]);   System.out.print("Commission\t "); 
    System.out.print(totalSalary);System.out.println("Difference from user");
Run Code Online (Sandbox Code Playgroud)

java if-statement

2
推荐指数
1
解决办法
192
查看次数

Selenium Chrome Driver不尊重隐含的等待?

好的!所以我在Windows 8上使用硒铬驱动程序(32位).

我已将隐式等待设置如下:

DesiredCapabilities des=DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1366,768");
des.setCapability(ChromeOptions.CAPABILITY, options);
dvr= new ChromeDriver(des);
    driver = new EventFiringWebDriver(dvr);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

但在我的测试的某些时候,我得到一个staleElementException ...如下所示:

我并不太担心staleElementException,困扰我的是异常中的以下行:命令持续时间或超时:14毫秒

当超时已经隐含地设置为30秒时,为什么我得到14毫秒的超时....任何建议或解决方法将是appreaciate谢谢!

public static WebElement grabElementByPureXPath(String xpath)
{
    WebElement element = null;
    int attempts=1;
    try
    {
        while(attempts<7)
        {
            try
            {
                element=driver.findElement(By.xpath(xpath));
            }
            catch(StaleElementReferenceException e){}
                attempts++;
        }
    }
    catch(Throwable t)
    {
        try
        {
            element=driver.findElement(By.cssSelector(xpath));
        }
        catch(Throwable T)
        {
            takeScreenShot(xpath);
            Assert.assertTrue(t.getMessage(),false);
        }
    }

    return element;
}
Run Code Online (Sandbox Code Playgroud)

<-----------------------以下例外情况----------------------- ---------------------->

org.openqa.selenium.StaleElementReferenceException: stale element reference: element   is not attached …
Run Code Online (Sandbox Code Playgroud)

java selenium-webdriver

1
推荐指数
1
解决办法
5582
查看次数

使用JSON Pretty Print的优缺点?

目前,我正在分析以不同格式生成JSON输出的优缺点.根据你的说法,JSON输出的优点和缺点是漂亮的打印和没有任何格式的相关的漂亮打印相关的东西,如

  1. 性能.

  2. 序列化/反序列化.

  3. 解析或任何其他标准.

我正在使用Java.

java json pretty-print

1
推荐指数
1
解决办法
624
查看次数

编码的有效解决方案在Java中谜语starOut

我说的问题是这个

问题陈述:

返回给定字符串的一个版本,对于字符串中的每个星号(*),星号和其左右两侧的字符都消失了.所以"ab*cd"产生"ad"和"ab**cd"也产生"ad".

starOut("ab*cd")→"ad"

starOut("ab**cd")→"ad"

starOut("sm*eilly")→"傻"

我得到的解决方案有点乱,我使用了大约6个if语句和处理一个特定情况,寻找更简洁的ifs更清晰的解决方案.

请不要使用超过1个循环和集合或正则表达式.

以下是我的解决方案:

public String starOut(String str) {
  String result="";
  boolean currentFlag=false;

  if(str.length()==0)
  return result;

  for(int i=0;i+1<str.length();i++)
  {
    if(str.charAt(i)=='*')
    {
       i+=1;
       currentFlag=true;
       continue;
     }
     if(str.charAt(i+1)=='*')
     {
        i+=2;
        continue;
     }
     if((i>0)&&str.charAt(i-1)=='*')
     {
        continue;
     }
     result+=str.substring(i,i+1);
     currentFlag=false;

   }

  if(!(currentFlag)&&(str.length()==3)&&(str.charAt(1)=='*'))
      return ""; 

   if((str.charAt(str.length()-1)!='*')&!(currentFlag))
     return result+=str.charAt(str.length()-1);
    else
      return result;
}
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
5232
查看次数

用Java中的一个循环解决codingBat Post4

问题是从Java中的codingBat 解决这个问题.

问题陈述:

给定一个非空的int数组,返回一个新数组,其中包含原始数组中最后4个之后的原始数组中的元素.原始数组将包含至少一个4.请注意,在Java中创建长度为0的数组是有效的.

post4({2,4,1,2})→{1,2}

post4({4,1,4,2})→{2}

post4({4,4,1,2,3})→{1,2,3}

这是我的解决方案:

public int[] post4(int[] nums) {

int lastFour=-1;
int[] post4={};

  for(int i=nums.length-1;i>=0;i--)
  {
     if((nums[i]==4))
     {
       lastFour=i;    //find the index of the last 4 in the array
       break;
     }  
   } 
   int newLen=(nums.length-lastFour)-1;
   post4=new int[newLen];   //reassign the post4 array with required length

  for(int j=0;j<newLen;j++)
  {
     post4[j]=nums[lastFour+1];   //assign values from orig. array after last 4
     lastFour++;
  }

  return post4;   
}
Run Code Online (Sandbox Code Playgroud)

但我使用了2个循环.它应该使用最多一个循环来解决.不要使用集合或任何包装类.

java

0
推荐指数
1
解决办法
2957
查看次数