使用espresso,如何根据显示的视图决定使用哪个视图操作?

时间:2016-08-11 08:13:04

标签: android android-espresso

我有一个表单,一次显示一个组件,它可能是:EditText,Spinner,ListView,或根据某些规则的其他内容。

每个组件都有不同的测试代码。

使用Espresso,如何查看显示的组件并执行其操作?

伪代码

if( ask to enter the number ){
    onView(withId(R.id.et_number)).perform(replaceText("12345"));
}else if(ask to select my country){
     onView(withRecyclerView(R.id.rv_country).atPosition(0)).perform(click());
}

实施此案例的最佳方法是什么?如果我必须创建自定义匹配器,那么这种情况的实现方式如何?

如果有人给我一个简单的例子,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

之前我做过类似的事情。我所做的是检查一个元素是否在try中显示并捕获异常。 试试这个:

--create tbl structure
create table #test (RequestID int, RequestStatus varchar(20), StartDate date, EndDate date)
go


--insert sample data
insert #test 
select 1,'pending','09/01/2015','10/2/2015'
union all
select 1,'in progress','10/2/2015','10/20/2015'
union all
select 1,'completed','10/20/2015','11/3/2015'
union all
select 1,'reopened','11/3/2015',null
union all
select 2,'pending','09/05/2015','9/7/2015'
union all
select 2,'in progress','09/07/2015','9/25/2015'
union all
select 2,'completed','9/25/2015','10/7/2015'
union all
select 2,'reopened','10/10/2015','10/16/2015'
union all
select 2, 'completed','10/16/2015','11/12/2015'
union all
select 2,'reopened','11/20/2015',null



select * from #test



--below is solution
select  a.RequestID, a.Startdate as [PendingStartDate], b.enddate as [FirstCompleteEndDate], c.startdate as [LatestReopenStartDate], 
        datediff(day,a.startdate,b.enddate)+c.ReopenDays as [days] from #test a
join (
    select *, row_number()over(partition by RequestID,RequestStatus order by StartDate) as rid from #test 
) as b
on a.RequestID = b.RequestID
join (
    select distinct RequestID, RequestStatus, max(StartDate)over(partition by RequestID,RequestStatus) as StartDate, 
            Sum(Case when enddate is null then datediff(day,startdate,getdate()) 
                  when enddate is not null then datediff(day,startdate,enddate) 
             end)over(partition by RequestID,RequestStatus) as [ReopenDays]
    from #test
    where RequestStatus = 'reopened'

) as c
on b.RequestID = c.RequestID
where   a.RequestStatus ='pending' and b.RequestStatus = 'completed' and b.rid = 1