You can create a task to ping Google DNS as follows:
Task → Code → Run Shell
Command: ping -c 2 8.8.8.8
[Store Output In: %stdout
] [Continue Task After Error: checked]
If
%stdout
~ 100% packet lossgoogle servers unreachable
Else
connectivity established
End If
Add this to your main task with either keeps wi-fi conection or switches it off.
Credits