2017-09-25 5 views
0

나는 Paint와 비슷한 GWT Web Application을 테스트 중이다. 여기에는 제가 수행하고있는 테스트 케이스가 있습니다. 셀레늄 그리드를 사용하여 다른 머신에서 이것을 테스트해야합니다. 이와 함께 TestNG를 사용하여 2 개의 브라우저에서이 테스트 케이스를 병렬로 실행하여이 테스트 케이스를 테스트하고있다. 자,이 테스트 케이스에서 직면하고있는 문제는 화면의 스크린 샷을 찍은 다음 현재 작업 디렉토리에서 저에게 만든 폴더에 로컬로 저장해야한다는 것입니다. 그러나 테스트 중에 두 번째 브라우저에서 찍은 스크린 샷은 두 브라우저 모두에 대해 하나의 클래스 (테스트 할 클래스) 만 가지고 있기 때문에 첫 번째 브라우저에서 사용한 스크린 샷을 업데이트합니다.TestNG + Selenium Grid를 사용하여 모든 병렬 테스트에서 새 파일을 만드는 방법은 무엇입니까?

내가 원하는 것은 스크린 샷을 찍어 내 로컬 디렉토리에 다른 이름으로 저장할 수 있다는 것입니다. 나는 인터넷에서 많은 것을 찾았지 만 그 방법들 중 어느 것도 나를 위해 잘 해결되지 못했고 따라서 이것을 어떻게하는지 혼란 스럽다.

한편, 여기에 내 코드가 있습니다.

먼저 브라우저의 기능과 함께 노드 주소를 지정하는 브라우저 클래스가 있습니다.

package testNgPackage; 

public class Browser { 
    //ThreadLocal will provide thread-safe tests 
    protected ThreadLocal<RemoteWebDriver> threadLocal = null; 
    @BeforeTest 
    @Parameters("browser") 
    public void setup(String browser) throws MalformedURLException{ 
     String nodeMachine = "http://xxx.xxx.xxx.xxx:5555/wd/hub"; //Providing the IP address of Node 
     if(browser.equalsIgnoreCase("chrome")) { 

      System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe"); 
      DesiredCapabilities capability = null; 
      capability = DesiredCapabilities.chrome(); 
      capability.setPlatform(Platform.VISTA); 
      capability.setBrowserName("chrome"); 
      threadLocal = new ThreadLocal<RemoteWebDriver>(); 
      threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability)); 
     } 
     else if(browser.equalsIgnoreCase("firefox")) { 

      System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe"); 
      DesiredCapabilities capability = null; 
      capability = DesiredCapabilities.firefox(); 
      capability.setPlatform(Platform.VISTA); 
      capability.setBrowserName("firefox"); 
      threadLocal = new ThreadLocal<RemoteWebDriver>(); 
      threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability)); 
     } 

    } 

    public WebDriver getDriver() { 
     return threadLocal.get(); 
    } 


    @AfterTest 
    public void closeBrowser(){ 
     getDriver().close(); 
    } 

} 

그런 다음 테스트 할 코드가 들어있는 TestNGClass가 있습니다.

package testNgPackage; 

public class TestNGClass extends Browser{ 

    @Test 
    public void testCanvas() throws InterruptedException{ 
     getDriver().manage().window().maximize(); 
     getDriver().manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ; 
     getDriver().get("XXXX"); //Here I have specified the URL 

     WebDriverWait wait = new WebDriverWait(getDriver(), 10); 
     wait.until(ExpectedConditions.titleContains("xxxxx")); 
     wait.until(ExpectedConditions.elementToBeClickable(By.id("yyyyy"))); 
     Thread.sleep(3000); 
     /* 
      After that, there is a code to click on certain elements and then take the screenshot of the canvas. 
      I won't mention the code here because of the privacy concerns of the company. Along with that, it is useless and wastage of time to mention it here. 
     */ 

     //Given below is the main code which describes how I've specified the file location of Images. 

     File elementImage; 
     File fileBase = new File("./src/Images/baseDrawing.png"); 
     File fileActual = new File("./src/Images/actualDrawing.png"); 
     try { 
      elementImage = this.takeElementScreenshot(canvas,"png"); 
      FileUtils.copyFile(elementImage, fileActual); 
     }catch(IOException e) { 
      e.printStackTrace(); 
     } 
     this.compareImage(fileBase,fileActual); 
     Thread.sleep(6000); 
    } 


    public File takeElementScreenshot(WebElement element, String imageFormat) throws IOException{ 
     /* 
      Code to take the Element Screenshot. The code in this function is correct and I'm not mentioning it here to reduce the reading time of the users. 

     */ 
     } 

    public void compareImage(File fileBase, File fileActual) { 
     /* 
     Code to compare the images by comparing them pixel by pixel. Not mentioning it here. 
     */ 

     } 
} 

이것은 매개 변수와 함께 실행할 테스트 이름을 지정한 testng.xml 파일입니다.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Parallel test suite" parallel="tests" thread-count="2"> 
    <test name="FirefoxTest"> 
     <parameter name="browser" value="Firefox" /> 
     <classes> 
     <class name="testNgPackage.TestNGClass"/> 
     </classes> 
    </test> 
    <test name="ChromeTest"> 
     <parameter name="browser" value="Chrome" /> 
     <classes> 
     <class name="testNgPackage.TestNGClass"/> 
     </classes> 
    </test> 
</suite> 

저는 TestNG의 초보자이므로 문제를 해결하는 방법을 혼동합니다. 도와주세요.

답변

1

실제 이미지 파일을 다른 이름으로 저장하려면. 아래 주어진대로 세션 ID 또는 브라우저 이름으로 저장할 수 있습니다.

방법 1 : 또는 그것은 당신을 도울 수

Capabilities cap = ((RemoteWebDriver) driver).getCapabilities(); 
String browserName = cap.getBrowserName().toLowerCase(); 
File fileActual = new File("./src/Images/actualDrawing"+browserName+".png"); 

로 교체 : 그냥

String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); 
File fileActual = new File("./src/Images/actualDrawing"+sessionId +".png"); 

방법 2로 다음 코드

File fileActual = new File("./src/Images/actualDrawing.png"); 

를 교체합니다.