Search code examples
angulartypescriptjasmineprotractorwebautomation

Using protractor typescript, how do i detect this element square white box, then click and drag the white box, it has ng-mousedown and transform


I need help in creating a typescript/javascript protractor code for this? for web automation.

How do I click on this whitebox and dragging it to left to right?

Dragging left to right, will also change the value of from transform="translate(205)" to transform="translate(206)" and also the <text>2/20/18</text>.

The web site code:

<g id="IndicateNav" ng-attr-trnsform="translate({{trVM._indicate || 0}})" ng-mousedown="trVM.mousedown($event, 'indicator')" ng-show="trVM._indicate" class="move-horizonal show-label-on-hover" transform="translate(205)">
   <line x1="0" x2="0" ng-attr-y1="{{trVM.bTop}}" ng-attr-y2="{{trVM.height}}" stroke="black" stroke-width="2" y1="20" y2="45"></line>
        <g ng-attr-trnsform="translate(0,{{trVM.bTop + ( (trVM.height - trVM.bTop) / 2) }})" transform="translate(0,32.5)">
            <square sx="0" sy="0" r="6" fill="white" stroke="black" stroke-width="2" ng-non-bindable=""></square>
        </g>
            <text x="14" ng-attr-y="{{3 + trVM.bTop + ( (trVM.height-trVM.bTop) / 2 )}}" text-anchor="start" font-size="12px" fill="white" style="pointer-events:none;" y="35.5">2/20/18</text>
</g>

Solution

  • You can take advantage of functions provided by browser.actions().

    Solution:

    1. Add id to square tag [Let's say the id is 'whiteBox'].
    2. Get 'whiteBox' element and save it to a variable.

      const whiteBox = element(by.css('#whiteBox'));
      
    3. Simulate the user's dragging behaviour by the following code:

      browser.actions().mouseDown(whiteBox).mouseMove({x: 50, y: 0}).mouseUp().perform()  
      

      This will move drag the whiteBox to 50 pixels right.

    4. If you want to drag 'whiteBox' to some other element, you can do that too:

      browser.actions().mouseDown(whiteBox).mouseMove(element2).mouseUp().perform()  
      

    Complete Test Code:

    describe('whiteBox Dragging Test', function() {
      it('Drag whiteBox to right', function () {
        const EC = protractor.ExpectedConditions;
        const whiteBox = element(by.css('#whiteBox'));
        browser.wait(EC.presenceOf(whiteBox), 5000).then(() => {
          browser.actions().mouseDown(whiteBox).mouseMove({x: 50, y: 0}).mouseUp().perform();
        });
      });
    });