Selenium Grid并行测试不兼容

时间:2018-01-31 08:52:06

标签: java selenium testing parallel-processing selenium-grid

您好我有两个名称为Node2Node1的节点,我在一台计算机上执行此测试。我的问题是Node2Node1测试不能同时工作,所以它们不是并行的。因此,Node2parallel="tests"测试开始后完成,但我不想要它,我希望它们一起开始。我已经在{{parallel="classes"TestNG.xml中尝试了Node1.java 1}}文件。

这是我的package grid; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.Test; public class Node1 { WebDriver driver; String nodeUrl; @Test public void f() { try { //configuration nodeUrl= "http://192.168.56.1:5555/wd/hub"; DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setBrowserName("chrome"); capabilities.setPlatform(Platform.WIN10); driver = new RemoteWebDriver(new URL(nodeUrl), capabilities); //test scripts driver.manage().deleteAllCookies(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS); driver.get("https://www.amazon.com/"); driver.findElement(By.linkText("Today's Deals")).click(); driver.findElement(By.linkText("Gift Cards")).click(); driver.findElement(By.linkText("Today's Deals")).click(); driver.findElement(By.linkText("Gift Cards")).click(); driver.findElement(By.linkText("Today's Deals")).click(); } catch (MalformedURLException e) { e.printStackTrace(); } } } 文件:

Node2.java

这是我的package grid; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.testng.annotations.Test; public class Node2 { WebDriver driver; String nodeUrl; @Test public void f() { try { //configuration nodeUrl= "http://192.168.56.1:5555/wd/hub"; DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setBrowserName("chrome"); capabilities.setPlatform(Platform.WIN10); driver = new RemoteWebDriver(new URL(nodeUrl), capabilities); //test scripts driver.manage().deleteAllCookies(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS); driver.get("https://www.google.com/"); } catch (MalformedURLException e) { e.printStackTrace(); } } } 文件:

TestNG.xml

这是我的<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Test Grid" parallel="tests"> <test name="Test Node1"> <classes> <class name="grid.Node1" /> </classes> </test> <test name="Test Node2"> <classes> <class name="grid.Node2" /> </classes> </test> </suite> 文件:

private void GetImage(){ 
                Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                    MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            pickPhoto.setType("image/*");

            //one can be replaced with any action code
            startActivityForResult(pickPhoto, 1);
}


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
      switch (requestCode){
        case 1:
            //Gallery
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                setPicforGallery(selectedImage);
            }
            break;
      }
}

private void setPicforGallery(Uri selecteduri) {


    int targetW = 200;
    int targetH = 200;

    Bitmap imageBitmap = null;
    String picturePath = getRealPathFromURI(selecteduri,
            this);

    try {
        imageBitmap = decodeSampledBitmapFromFile(picturePath,targetW,targetH);
    }
    catch (Exception e){

        Toast.makeText(getApplicationContext(),"Cannot convert to bitmap",Toast.LENGTH_LONG).show();
    }

    if(imageBitmap == null){
        Log.v("image","image bitmap is null");
    }

}



public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

    // BEST QUALITY MATCH

    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight)
    {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth)
    {
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);

}


public static String getRealPathFromURI(Uri contentURI, Activity context) {
    String result = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver( ).query( contentURI, proj, null, null, null );
    if(cursor != null){
        if ( cursor.moveToFirst( ) ) {
            int column_index = cursor.getColumnIndexOrThrow( proj[0] );
            result = cursor.getString( column_index );
        }
        cursor.close( );
    }
    if(result == null) {
        result = "Not found";
    }
    return result;
}

3 个答案:

答案 0 :(得分:1)

尝试下面的testNG。xml`文件,对我有用。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" parallel="classes" thread-count="2">
    <classes>
      <class name="com.github.parallel.Parallel1"/>
      <class name="com.github.parallel.Parallel2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

答案 1 :(得分:0)

尝试以下xml代码。

use Illuminate\Support\Facades\Schema;

答案 2 :(得分:0)

如果在两个测试中将WebDriver字段声明移动到@Test方法中。它也应该使用parallel =“methods”。字段不是线程安全的。

 @Test
public void f() {
    try {
        WebDriver driver;
        //configuration
        nodeUrl= "http://192.168.56.1:5555/wd/hub";
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setBrowserName("chrome");
        capabilities.setPlatform(Platform.WIN10);
        driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);

        //test scripts
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
}