如何在Appium中从右向左滚动布局视图?

时间:2019-05-09 10:44:27

标签: eclipse appium

想要在Appium中从右向左滑动视图,我尝试使用坐标,并且还动态尝试过,但是不可能滑动视图,每当我滑动视图时,都会出现一个图标,我想单击图标。

我的测试用例已执行,但无法滑动视图。 我通过使用co_ordinates尝试了TouchAction。

         Boolean found_result = false;
         String a = "offlin";
         List<AndroidElement> listele = driver.findElementsByClassName("android.widget.LinearLayout");
         System.out.println("swipe = "+listele.size()); 

         int size=0;
         size = size+listele.size();
         AndroidElement slider = listele.get(0);
         Point startButtonPoint = slider.getLocation();
            Dimension startButtonDimenstion = slider.getSize();
         int y = startButtonPoint.getY() + startButtonDimenstion.getHeight()/2;
            int xStart = startButtonPoint.getX() + (int)(startButtonDimenstion.getWidth() * 0.7);
            int xEnd = (int)(startButtonPoint.getX() * 0.3);
         System.out.println("lists ele size"+size);
         for (int i = 0; i < size; i++) {

             String s = listele.get(i).getText();
             if (s.equals(a)) {

                 found_result =true;

                 driver.swipe(xStart, y, xEnd, y, 2000);
                  System.out.println("found : "+size);
                 break;
             }

         }
         if(!found_result){
             System.out.println("swipped cond");
      }

正如我期望的那样,要从右向左滑动视图,并在看到一个视图后,想要单击该视图以完成任务。

3 个答案:

答案 0 :(得分:1)

  1. 您可以考虑执行mobile:swipe command,例如:

    [HttpPost]
            [AllowAnonymous]
            public IActionResult Search([FromBody] SearchModel model)
            {    try
                { var query = _service.AllAsQueryable;
    
                    if (model != null)
                    {
                        if (!string.IsNullOrEmpty(model.Keyword))
                        { 
    
    
                            var keyword = model.Keyword.ToLower();
                            query = query.Where(a => a.BuildTitle.ToLower().Contains(keyword) ||
                                                     a.Description.ToLower().Contains(keyword) ||
                                                     a.ZipCode.ToLower().Contains(keyword) ||
                                                    // (a.Country != null && a.Country.Localizations.Any(b => b.Name.ToLower().Contains(keyword))) ||
                                                     (a.Region != null && a.Region.Localizations.Any(b => b.Name.ToLower().Contains(keyword))) ||
                                                     (a.District != null && a.District.Localizations.Any(b => b.Name.ToLower().Contains(keyword))) ||
                                                     (a.Zone != null && a.Zone.Localizations.Any(b => b.Name.ToLower().Contains(keyword))));
    
    
                        }
    
                        if (model.RegionId.HasValue && model.RegionId > 0)
                            query = query.Where(a => a.RegionId == model.RegionId);
    
                        if (model.DistrictId.HasValue && model.DistrictId > 0)
                            query = query.Where(a => a.DistrictId == model.DistrictId);
    
                        if (model.ZoneId.HasValue && model.ZoneId > 0)
                            query = query.Where(a => a.ZoneId == model.ZoneId);
    
                        if (model.ClientTypeId.HasValue && model.ClientTypeId > 0)
                            query = query.Where(a => a.ClientTypeId == model.ClientTypeId);
    
                        if (model.PriceFrom.HasValue)
                            query = query.Where(a => a.OwnerPrice >= model.PriceFrom);
    
                        if (model.PriceTo.HasValue && model.PriceTo > 0)
                            query = query.Where(a => a.OwnerPrice <= model.PriceTo);
    
                        if (model.Size != null && model.Size.SizeNameId > 0)
                        {
                            query = query.Where(a => a.SizeNameId == model.Size.SizeNameId);
    
                            if (model.Size.SizeFrom.HasValue)
                                query = query.Where(a => a.SizeNameId == model.Size.SizeNameId && a.Size >= model.Size.SizeFrom && a.Size <= model.Size.SizeTo);
                        }
    
                        if (model.BuildActionTypes?.Length > 0)
                            query = query.Where(a => a.BuildAction != null && model.BuildActionTypes.Contains(a.BuildAction.Id));
    
                        if (model.BuildTypes?.Length > 0)
                            query = query.Where(a => a.BuildType != null && model.BuildTypes.Contains(a.BuildType.Id));
    
                        if (model.Rooms?.Length > 0)
                        {
                            foreach (var room in model.Rooms)
                            {
                                query = query.Where(a => a.BuildingRooms.Any(b => b.RoomType == room.Type && room.Count.Contains(b.RoomCount)));
                            }
                        }
    
                        switch (model.SortType)
                        {
                            case SortType.Asc:
                                query = query.OrderBy(a => a.OwnerPrice);
                                break;
                            case SortType.Desc:
                                query = query.OrderByDescending(a => a.OwnerPrice);
                                break;
                            case SortType.Newest:
                                query = query.OrderByDescending(a => a.CreatedDate);
                                break;
                        }
    
                        if (model.Points != null)
                        {
                            query = query.Where(a => a.Latitude.HasValue && a.Latitude >= model.Points.X1 && a.Latitude <= model.Points.X2 &&
                                                     a.Longitude.HasValue && a.Longitude >= model.Points.Y1 && a.Longitude <= model.Points.Y2);
                        }
                    }
                    var totalCount = query.Count();
                    var result = query.Skip(model.PageSize * (model.CurrentPage - 1))
                                      .Take(model.PageSize)
                                      .AsEnumerable()
                                      .Select(a =>
                                      {
                                          var building = a.MapTo<BuildingShortDetailsViewModel>();
                                          building.LoadEntity(a);
                                          return building;
                                      });
                    var searchResult = new SearchResult
                    {
                        Filter = model,
                        Locations = query.Select(a => new LocationModel
                        {
                            BuildingId = a.Id,
                            Latitude = a.Latitude.ToString(),
                            Longitude = a.Longitude.ToString(),
                            OwnerPrice= a.OwnerPrice,
                            Size= a.Size,
                            SizeName= a.SizeName.Id,
                            BuildingPhotos= a.BuildingPhotos,
                            BuildAction=a.BuildAction.Id,
                        }),
                        Buildings = result,
                        TotalCount = totalCount
                    };
    
                    return Ok(searchResult);
                }
                catch (Exception ex)
                {
                    return BadRequest($"Error occured while getting search result. Error: {ex.Message}. StackTrace: {ex.StackTrace}");
                }
            }
    
  2. 如果您的测试仅适用于Android,则可以考虑执行mobile:shell command

    JavascriptExecutor js = (JavascriptExecutor) driver;
    Map<String, Object> params = new HashMap<>();
    params.put("direction", "down");
    params.put("element", ((RemoteWebElement) element).getId());
    js.executeScript("mobile: swipe", params);
    
  3. 最后但并非最不重要的一点,您可以使用提供SeeTest Appium Extension

    Swipe command
    Map<String, Object> args = new HashMap<>();
    args.put("command", "input");
    args.put("args", Lists.newArrayList("swipe", "startX","startY","endX","endY"));
    driver.executeScript("mobile: shell", args);
    

答案 1 :(得分:0)

使用以下代码在可用视图中执行向右滑动。

MobileElement element_to_be_swiped=driver.findElement("//xpathtothelinearpath")
Point elementLoc = elememt_to_be_swiped.getLocation();
int eleX = elementLoc.getX() + 5;
int eleY = elementLoc.getY() + 5;
Dimension elementDimen = elememt_to_be_swiped.getSize();
int eleH = elementDimen.getHeight();
int eleW = elementDimen.getWidth();
Dimension size = driver.manage().window().getSize();
int x = (int)(eleX + eleW * 0.5D);
int y = (int)(eleY + eleH * 0.5D);

TouchAction<?> action = new TouchAction(driver);
action.press(PointOption.point(x, y)).moveTo(PointOption.point(width - 5, y)).release().perform();

答案 2 :(得分:0)

下面是我用来滚动和滑动的代码。请注意,有一个主滚动方法,该方法使用由四个方向滚动/滑动方法中的每个方法计算出的适当参数来调用。

/**
 * This method scrolls based upon the passed parameters
 * @author Bill Hileman
 * @param int startx - the starting x position
 * @param int starty - the starting y position
 * @param int endx - the ending x position
 * @param int endy - the ending y position
 */
@SuppressWarnings("rawtypes")
public void scroll(int startx, int starty, int endx, int endy) {

    TouchAction touchAction = new TouchAction(driver);

    touchAction.longPress(PointOption.point(startx, starty))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

/**
 * This method does a swipe upwards
 * @author Bill Hileman
 */
public void scrollDown() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 80% of the height (near bottom)
    int starty = (int) (size.height * 0.80);
    //Ending y location set to 20% of the height (near top)
    int endy = (int) (size.height * 0.20);
    //x position set to mid-screen horizontally
    int startx = (int) size.width / 2;

    scroll(startx, starty, startx, endy);

}

/**
 * This method does a swipe left
 * @author Bill Hileman
 */
public void swipeLeft() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting x location set to 95% of the width (near right)
    int startx = (int) (size.width * 0.95);
    //Ending x location set to 5% of the width (near left)
    int endx = (int) (size.width * 0.05);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);

}

/**
 * This method does a swipe right
 * @author Bill Hileman
 */
public void swipeRight() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting x location set to 5% of the width (near left)
    int startx = (int) (size.width * 0.05);
    //Ending x location set to 95% of the width (near right)
    int endx = (int) (size.width * 0.95);
    //y position set to mid-screen vertically
    int starty = size.height / 2;

    scroll(startx, starty, endx, starty);

}

/**
 * This method does a swipe downwards
 * @author Bill Hileman
 */
public void scrollUp() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 20% of the height (near bottom)
    int starty = (int) (size.height * 0.20);
    //Ending y location set to 80% of the height (near top)
    int endy = (int) (size.height * 0.80);
    //x position set to mid-screen horizontally
    int startx = size.width / 2;

    scroll(startx, starty, startx, endy);

}
相关问题